Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want all the features of the generic view then you can just create a new generic view using the original as a template.</p> <p>Eg:</p> <pre><code>def create_object_with_initial(request, model=None, template_name=None, template_loader=loader, extra_context=None, post_save_redirect=None, login_required=False, context_processors=None, form_class=None, initial=None): if extra_context is None: extra_context = {} if login_required and not request.user.is_authenticated(): return redirect_to_login(request.path) model, form_class = get_model_and_form_class(model, form_class) if request.method == 'POST': form = form_class(request.POST, request.FILES) if form.is_valid(): new_object = form.save() msg = ugettext("The %(verbose_name)s was created successfully.") %\ {"verbose_name": model._meta.verbose_name} messages.success(request, msg, fail_silently=True) return redirect(post_save_redirect, new_object) else: print "creating", form_class, " with initial data ", initial form = form_class(initial=initial) # Create the template, context, response if not template_name: template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower()) t = template_loader.get_template(template_name) c = RequestContext(request, { 'form': form, }, context_processors) apply_extra_context(extra_context, c) return HttpResponse(t.render(c)) </code></pre> <p>This is copied from /site-packages/django/views/generic/create_update.py with only lines 3 and 21 changing to incorporate the initial data.</p> <p>Then use it as you might expect:</p> <pre><code>object_info = { 'model': YourModel, 'initial': {'data' : 'Initial Value'}, 'template_name': 'template.html' } url(r'^path/$', login_required(create_object_with_initial), object_info, name='url_name'), </code></pre> <p>That should work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload