Within a template distinguish between create and update form

前端 未结 3 1594
轻奢々
轻奢々 2021-01-01 16:13

If the CreateView and UpdateView are using the same template \"model_form.html\" then within the template how would I differentiate if I am creating or updating a form?

3条回答
  •  情话喂你
    2021-01-01 17:04

    add this function in your both CreateView and UpdateView class:

    # For Create
    def get_context_data(self, **kwargs):
            kwargs['naming'] = 'Create'
            context = super(CLASSNAME, self).get_context_data(**kwargs)
            return context
    
    # For Update
    def get_context_data(self, **kwargs):
            kwargs['naming'] = 'Update'
            context = super(CLASSNAME, self).get_context_data(**kwargs)
            return context
    

    then reference those in your template with {{ naming }}

    example

    
    

提交回复
热议问题