Answer the question
In order to leave comments, you need to log in
Django how to pass parameter from template context to url?
Sample:
<form action="{% url 'new_item' category=category %}" method="post">
url(r'^new/(?P<category>[\w\-]+)/$', views.new_item, name='new_item'),
def new_item(req, category=''):
args = dict()
args.update(csrf(req))
if req.method == 'POST':
if category == 'phone':
form = PhoneForm(req.POST, req.FILES)
if form.is_valid():
form.save()
return redirect('general')
else:
args['form'] = form
return render_to_response('new_item/index.html', args)
else:
if category == 'phone':
args['form'] = PhoneForm()
args['category'] = 'phone'
return render_to_response('new_item/index.html', args)
<a href="{% url 'new_item' category=category %}"></a>
NoReverseMatch at /adminpanel/new/phone/ Reverse for 'new_item' with arguments '()' and keyword arguments '{'category': ''}' not found. 1 pattern(s) tried: ['adminpanel/new/(?P[\w\-]+)/$']
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question