3

私はこのフォームを持っています:

<form action="{% url create_question %}" method="post">

そしてこのurl.py

url(r'^neues_thema/(\w+)/','home.views.create_question',name="create_question"),

しかし、私はこのエラーが発生しています:

Reverse for 'create_question' with arguments '()'
 and keyword arguments '{}' not found.

私は何を間違っていますか?

編集:私がやりたいことは:ユーザーがフォームを送信し、ユーザーが作成している質問のタイトルを取り、それをURLに入れたいです。URL は次のようになりますneues_thema/how-to-make-bread/{% url create_question ??? %}フォームの送信中にそのパラメーターを動的に与えるにはどうすればよいですか

djangoテンプレートのこのスレッドURLテンプレートタグは役に立ちませんでした。

4

4 に答える 4

3

URL 正規表現にはパラメーターが必要です。テンプレートは次のようにする必要があります。

<form action="{% url create_question some_user_name %}" method="post">

組み込みのテンプレート タグとフィルターに関するドキュメントのURLを参照してください。

于 2013-05-25T17:50:16.463 に答える
1

{% url %}テンプレートにパラメーターは必要ないようです。

views.py成功後にユーザーを質問ページにリダイレクトする質問を作成するための機能をに追加できます。

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"),

ビュー.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})

質問を作成するためのフォームを含むテンプレート:

<form action="{% url create_question %}" method="post">

あなたの「私は何を間違っていますか?」に答えます。これでマスクでURLをレンダリングしようとしていますneues_thema/(\w+)/: {% url create_question %}. マスクにはいくつかのパラメーター ( (\w+)) が必要ですが、パラメーターを入れていません。パラメータを使用したレンダリングは{% url create_question title %}. titleしかし、問題は、レンダリング中のページがわからないことです。

于 2013-05-25T18:48:20.867 に答える
0

このように書き{% url 'home.views.create_question' alhphanumeric_work %}ます。それはうまくいくはずです。

于 2013-05-25T18:27:20.803 に答える