1
Seems like you donโt need any parameters to {% url %}
in your template.
You can add function to your views.py
for creating questions, that will redirect user to question page after success:
urls.py:
url(r'^neues_thema/', 'home.views.create_question', name="create_question"),
url(r'^neues_thema/(?P<title>\w+)/', 'home.views.question', name="question"),
views.py:
from django.core.urlresolvers import reverse
from django.shortcuts import render
def create_question(request):
if request.method == 'POST':
title = request.POST['title']
# some validation of title
# create new question with title
return redirect(reverse('question', kwargs={'title': title})
def question(request, title):
# here smth like:
# question = get_object_or_404(Question, title=title)
return render(request, 'question.html', {'question': question})
template with form for creating question:
<form action="{% url create_question %}" method="post">
Answering your โwhat am i doing wrong?โ. You are trying to render url by mask neues_thema/(\w+)/
with this: {% url create_question %}
. Your mask needs some parameter ((\w+)
), but you are putting no parameter. Rendering with parameter should be {% url create_question title %}
. But the problem is: you donโt know the title
while rendering page.
3
Your url regular expression expects a parameter, your template should be like:
<form action="{% url create_question some_user_name %}" method="post">
- [Django]-Django: Stacking decorators
- [Django]-How to do an exclude django query on multiple foreign key
- [Django]-TemplateSyntaxError: Could not parse the remainder
- [Django]-Django-pipeline 'compressed' is not a valid tag library: ImportError raised loading pipeline.templatetags.compressed: No module named conf
2
You can do:
url(r'^neues_thema/(?P<user>\w*)$','home.views.create_question',name="create_question"),
and in your views
def create_question(request, user=None):
- [Django]-How to call multiple views on one url in pinax
- [Django]-Django: request.META['REMOTE_ADDR'] is always '127.0.0.1'
- [Django]-How to call model methods in template
- [Django]-How to skip a scenario in Lettuce?
- [Django]-DJango URL Reverse Error: argument to reversed() must be a sequence
0
Write it like this {% url 'home.views.create_question' alhphanumeric_work %}
. It should work.
- [Django]-Define and insert age in django template
- [Django]-How to get a word count on word document in python?
- [Django]-Django all-auth: How to disable automatic login via Google