动态删除Djang中的下拉选项

2024-10-02 08:17:33 发布

您现在位置:Python中文网/ 问答频道 /正文

当一个用户没有经过身份验证时,我需要从下拉列表中隐藏一个选项。你知道吗

class JobPostingListView(ListView):
    form_class = JobPostingSearchForm
    model = JobPosting
    template_name = 'recruiters/job_posting_list.html'

    def get_queryset(self):

        form = self.form = self.form_class(self.request.GET)

    ......


    def get_context_data(self, **kwargs):

        context_data = super(JobPostingListView, self).get_context_data(**kwargs)

        if not self.request.user.is_authenticated():
            del self.form.fields['date_posted'].choices[1]

        #Ok, It shows all except choice one
        print self.form.fields['date_posted'].choices 

        #It doesn't work here because it shows all the choices 
        print self.form                               

        context_data.update({
            'form': self.form                         
        })

        return context_data

注意:该代码在视图中,因为我无权访问表单中的is_authenticated。你知道吗

窗体显示所有选项,我想隐藏选项一。你知道吗


Tags: selfformfieldsdatagetisrequestdef
1条回答
网友
1楼 · 发布于 2024-10-02 08:17:33
  1. 重写paintComponent而不是paint并调用super.paintComponent(g)

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ...
    }
    
  2. JPanel覆盖getPreferredSize()

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }
    
  3. 不要paintComponent方法中创建图像。在构造函数中执行

    public class GameBoard extends JPanel { 
        ImageIcon img;
        public GameBoard() {
            img = new ImageIcon(...);
        }
    
  4. pack()您的帧,setSize(),并且JPanel的首选大小将得到尊重(通过您的覆盖getPreferredSize()

  5. 你可以在覆盖getPreferredSize()之后使用getWidth()getHeight()

    drawImage(bg.getImage(), 0, 0, getWidth(), getHeight(), this);
    

做所有这些事情,如果没有帮助,发布一个我们可以测试的可运行的例子

相关问题 更多 >

    热门问题