簡體   English   中英

Django1.8'app'不是注冊名稱空間

[英]Django1.8 'app' is not a registered namespace

我的錯誤:

    <h1>{{ question.question_text }}</h1>
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

    <form action="{% url 'app:vote' question.id %}" method="post">
    {% csrf_token %}
    {% for choice in question.choice_set.all %}
        <input type="radio" name="choice" id="choice{{ forloop.counter}}" value="{{ choice.id }}" />
        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
    {% endfor %}
    <input type="submit" value="Vote" />
    </form>

我想知道為什么這里錯了。 這是該文件的官方網站編寫的

這個detail.html

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'app:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}    </label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

現在我知道問題在detail.html ,我的主要網址和我的應用程序myapp URLCONF和views.py

views.py

def vote(request, question_id):
    p = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.html', {
            'question': p,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

我的urls.py

urlpatterns = [
    url(r'^$',views.Index,name='index'),
    url(r'^(?P<question_id>\d+)/$',views.detail,name='detail'),
    url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
    url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
    url(r'^admin/', admin.site.urls),
]

當您在根urls.py包含另一個url配置時,將使用命名空間。 本教程中的示例有效,因為它們在教程1中 include('polls.urls') ,然后在教程3中他們在polls/urls.py設置了app_name = 'polls'並將url標記更改為{% 'polls:vote' question.id %}

在您的情況下,您的所有網址格式都在根配置中,因此您應該從網址標記中刪除命名空間。

{% url 'vote' question.id %}

我的項目圖片

我添加了一個新的urls.py

這段代碼,mydjango / urls.py


from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^app/', include('app.urls')),
    url(r'^admin/', admin.site.urls),
]

應用程序/ urls.py


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM