Django: common view-UpdateView updates form class with parameters, django-updateview
This prompt is put at the beginning: to use a general view to update a form, the form type must be ModelForm,
That is:
class FileForm(forms.ModelForm): ....
Don't ask why I know
Bytes ------------------------------------------------------------------------------------------------------
First: urls. py
# Coding = utf-8from django. conf. urls import patterns, urlfrom app_filemanager import viewsurlpatterns = patterns ('',... url (R' ^ (? P <pk> \ d +)/update/$ ', views. fileUpdateView. as_view (), name = 'update'), # pk is used as the variable name for common view values, and this pk is usually the one generated by the model, therefore, use d + for regular expression matching)
Then: forms. py
# Coding = utf8from django import formsfrom. models import File, Project, Tagfrom django. contrib. auth. models import User, Groupclass FileForm (forms. modelForm): def _ init _ (self, user, * args, ** kwargs): super (FileForm, self ). _ init _ (* args, ** kwargs) self. fields ['tag'] = forms. modelChoiceField (queryset = Tag. objects. filter (owner = user), required = True, label = "classification", help_text = "cannot be blank, use for personal document classification", error_messages = {'required ': "The following are required items"}, empty_label = "Please select at least one", widget = forms. select (attrs = {'class': 'form-control', 'style': 'width: 100% ',}),) project = forms. modelChoiceField (queryset = Project. publicProjects. order_by ('-add_date'), required = False, label = "project", help_text = "can be empty. To prevent confusion, a document can only belong to one project ", error_messages = {'required': "The following are required."}, widget = forms. select (attrs = {'class': 'form-control', 'style': 'width: 100% ',}),) tag = forms. modelChoiceField (queryset = Tag. objects. none (),) group = forms. modelMultipleChoiceField (queryset = Group. objects. order_by ('-id'), required = True, label = "which groups are visible", help_text = "multiple options available", error_messages = {'required ': "select at least one of the following"}, widget = forms. selectMultiple (attrs = {'class': 'form-control', 'style': 'width: 100% '}),) desc = forms. charField (required = False, label = "Description", widget = forms. textarea (attrs = {'placeholder ': U' you can find the document', 'rows ': 2, 'style': 'width: 100%', 'class ': 'form-control',}),) file = forms. fileField (required = True, label = "document", help_text = "size limit: 1 GB", error_messages = {'required': "select a file "},) deleted = forms. booleanField (required = False, label = "marked as deleted when selected", help_text = "public by default, personal files are not classified by projects", widget = forms. hiddenInput (attrs = {'style': 'width: 100% ', 'class': 'form-control',}),) public = forms. booleanField (required = False, label = "public", # help_text = "selected as public, personal files are not classified by projects",) class Meta: model = File fields = ('project', 'tag', 'group', 'desc', 'file', 'public ')
For the init method in the form class, refer to my previous article. Here
In the previous blog, how does one upload request. user to form? Here, we will explain how to upload
Then: views. py
class FileUpdateView(UpdateView): model = File template_name_suffix = '_update_form' success_url = '/file/' permission_required = 'app_filemanager.can_change_file' permission_fail_message = ('You don\'t have permission to change employee info.') form_class = FileForm def get_form_kwargs(self): kwargs = super(FileUpdateView, self).get_form_kwargs() kwargs.update({ 'user':self.request.user }) return kwargs
Last: file_update_form.html
{% extends "base.html" %}{% block content_middle %}<form action="" method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" class="btn btn-default" value="Update" /></form>{% endblock %}
Attached:
This is simple, so it is easy to say that the form class of django will be used and applied flexibly. If you have any questions, leave a message.