Accessing URL variable from within a Form

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# urls.py
# URL1
url(r'^banker/(?P<iid>\d+)/iac$', 
    view = IacCreateView.as_view()),
# URL2
url(r'^banker/(?P<pk>\d+)/$', 
    view = BankerDetailView.as_view(),
    name='banker'),

# forms.py
class IaccountForm(forms.ModelForm):
    class Meta(object):
        model = Iaccount

# views.py
class IacCreateView(LoginRequiredMixin, CreateView):
    template_name = 'iaccount_form.html'
    model = Iaccount
    form_class = IaccountForm

    def get_initial(self):
        initials = super(IaccountCreateView, self).get_initial()
        initials['ident'] = self.kwargs['iid']
        return initials

    def get_success_url(self):
        pk = self.kwargs['iid']
        return reverse('banker', kwargs={'pk': pk})

# template iaccount_form.html
...
<a href="{% url 'banker' form.ident.value %}">go back</a>
...

More like this

  1. Complex Form Preview by smagala 5 years ago
  2. Complex Formsets, Redux by smagala 4 years, 1 month ago
  3. Improved Pickled Object Field by taavi223 4 years, 8 months ago
  4. @url decorator improvements by davepeck 4 years, 8 months ago
  5. Simple Plone Migration by msm-art 6 years, 3 months ago

Comments

justinguru (on September 10, 2013):

Thanks for nice tutorial. Wholesale Online Store

#

(Forgotten your password?)