Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango class based generic views: setting a field to initial value based on context
    primarykey
    data
    text
    <p>I have a set of related models in a Django 1.5 app: <code>Events</code> that have <code>EventSessions</code>, and <code>EventSessionRegistrations</code> that belong to <code>EventSessions</code>.</p> <p>I'm struggling to create a class based generic view for a user-facing registration form, where a user registers to attend an <code>EventSession</code>. Specifically the problem is around excluding the field for which <code>EventSession</code> the registration is for from being rendered in the form, whilst still setting that value based on the context and/or URL.</p> <p>I'll try to explain via a use-case first:</p> <ol> <li>User goes to <code>'/events/foo/bar'</code>, where <code>foo</code> is the <code>Event</code>, and <code>bar</code> is the <code>EventSession</code>.</li> <li>At this URL, there is a link to register for the <code>EventSession</code>, leading to <code>'/events/foo/bar/register/'</code>. Form for the <code>EventSessionRegistration</code> model is displayed, but without selecting the <code>EventSession</code> in the UI, since that information is already "set" by the URL.</li> <li>After successful form submission, user is redirected to a static "Thanks"-page.</li> </ol> <p>To acheive this, I have the following view code (lots of other imports etc excluded):</p> <pre><code>from django.views.generic.edit import CreateView class RegistrationCreate(CreateView): form_class = SessionRegistrationForm success_url = '/thanks/' template_name = 'events/registration_create.html' def get_context_data(self, *args, **kwargs): """Set the context of which Event, and which EventSession. Return 404 if either Event or EventSession is not public.""" context = super(RegistrationCreate, self).get_context_data(**kwargs) s = get_object_or_404( EventSession, event__slug=self.kwargs['event_slug'], event__is_public=True, slug=self.kwargs['session_slug'], is_public=True) context['session'] = s context['event'] = s.event return context </code></pre> <p>URL pattern for this view (included from a base <code>urls.py</code>):</p> <pre><code>url(r'^(?P&lt;event_slug&gt;[\w-]+)/(?P&lt;session_slug&gt;[\w-]+)/register/$', RegistrationCreate.as_view(), name="event_session_registration"), </code></pre> <p>In the ModelForm, I've tried to convert the ForeignKey <code>session</code> on EventSessionRegistration field (pointing to <code>EventSession</code>) to show a <code>HiddenInput()</code>-widget:</p> <pre><code>class SessionRegistrationForm(forms.ModelForm): class Meta: model = EventSessionRegistration widgets = { 'session': HiddenInput() } </code></pre> <p>But I still don't know how to set the initial value of that field to the id of the <code>'session'</code> value that I set in <code>get_context_data</code>. I've tried setting <code>self.initial = {'session': s.id}</code> inside <code>get_context_data</code>, but I'm guessing that the <code>initial</code> attribute is already used to construct the form at that point.</p> <p>Any ideas on the best way to acheive this? What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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