使用参数'()'和未找到关键字参数'{}'来反转'home'。尝试过0种模式:[]

时间:2016-05-02 19:19:56

标签: python django django-views django-urls

我的项目名称是'trydjango19',我有两个应用程序:'newsletter'和'posts'。

trydjango19 / urls.py是:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^posts/', include("posts.urls", namespace='posts')),
    url(r'^', include("newsletter.urls", namespace='newsletter')),]

newsletter / urls.py是:

urlpatterns = [
    url(r'^', 'newsletter.views.home', name='home'),]

newsletter / view.py是:

def home(request):
    title = 'ОСТАВЬТЕ ЗАЯВКУ'
    form = SignUpForm(request.POST or None)
    context = {
        "title": title,
        "form": form
    }
    if form.is_valid():
        instance = form.save(commit=False)

        full_name = form.cleaned_data.get("full_name")
        if not full_name:
            full_name = "anonymous"
        instance.full_name = full_name
        instance.save()
        context = {
            "title": "Ваша заявка принята!"
    }

    if request.user.is_authenticated() and request.user.is_staff:
        queryset = SignUp.objects.all().order_by('-timestamp') 
        context = {
            "queryset": queryset
        }

    return render(request, "newsletter/home.html", context)

newsletter / templates / newsletter / home.html是:

{% extends 'newsletter/base.html' %}
{% load crispy_forms_tags %}
{% load staticfiles %}

{% block head_title %}Welcome | {{ block.super }}{% endblock %}

{% block jumbotron %}
    {% if not request.user.is_authenticated %}

            <img class="close" onclick="show('none')" src="{% static 'img/close.png' %}">

            <p class='lead text-align-center'>{{ title }}</p>

            <form method='POST' action=''>{% csrf_token %}
                {{ form|crispy }}
                <p class='text-align-center'>
                    <input class='btn btn-primary' type='submit' value='Откликнуться' />
                </p>
            </form>
        </div>
<img src="{% static 'img/pony.png' %}" width='380px' />
... etc.

我尝试使用urls.py在很多方面,使用'namespace'和'app_name',但我仍然不明白它应该如何正常运行

In template /home/pavel/DJANG/django19/src/newsletter/templates/newsletter/home.html, error at line 0
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 个答案:

答案 0 :(得分:5)

我猜测您在url中使用了base.html标记,类似于:

{% url 'home' %}

但是在主urls.py文件中,您确定了urls.py app中的子newsletter文件的名称空间。

将此url标记更改为以下内容:

{% url 'newsletter:home' %}

或从url(r'^', include("newsletter.urls", namespace='newsletter')),]行中删除命名空间参数。

<强>注意:
设置home url模式的结尾更好,但这不会引发异常:

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