Pre-populating Model Form with object data - Django -
i have tried various options no luck far. trying instance data pre-populated modelfield. here have:
forms.py
class edit_project_info(modelform): project_name = forms.charfield(max_length=150) class meta: model = project exclude = ['project_type', 'created_date', 'start_date', 'end_date', 'pm_scope', 'dev_scope', 'design_scope', 'testing_scope' ]
view.py
def edit_project (request, offset): this_project = project.objects.get(pk=offset) data = {'project_name' : 'abc'} if request.method == 'post': form = edit_project_info(request.post, instance=this_project, initial=data) if form.is_valid(): form.save() return httpresponseredirect('/project_profile/%s/' % offset) else: form = edit_project_info()
all empty field. can add initial value forms.py, static rather populated based on form instance. have done here creating dict , passing initial in form instance not seem anything. i'm sure missing basic. great! ahead of time.
two last lines recreate form
variable. remove else: form = edit_project_info()
:
def edit_project (request, offset): this_project = project.objects.get(pk=offset) data = {'project_name' : 'abc'} form = edit_project_info(request.post, instance=this_project, initial=data) if request.method == 'post': if form.is_valid(): form.save() return httpresponseredirect('/project_profile/%s/' % offset) # else: # form = edit_project_info() # ...
Comments
Post a Comment