python - 反转 'home',参数 '()' 和关键字参数 '{}' 未找到。 0 种模式已尝试 : []

标签 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,使用“命名空间”和“应用程序名称”,但我仍然不明白它应该如何正确运行

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: []

最佳答案

我推测您在 base.html 中使用了 url 标签,类似如下:

{% url 'home' %}

但在主 urls.py 文件中,您为 newsletter 应用程序中的子 urls.py 文件确定了 namespace 。

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

{% url 'newsletter:home' %}

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


注意事项:
设置结束主页 url 模式更好,但这不会引发异常:

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

关于python - 反转 'home',参数 '()' 和关键字参数 '{}' 未找到。 0 种模式已尝试 : [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36990017/

相关文章:

python - 在纯 NumPy 中重写 for 循环以减少执行时间

python - 如何动态训练 LSTM 模型?

python - 在 django 模型中,如何检查 ManyToMany 字段中的值集的唯一性?

django - 如何在 Django Rest Framework (DRF) 中控制版本控制

python - Django - url 中的 View 或条件根据参数是否具有值或为 NONE 选择两种 url 模式之一

django - 应该对 Django 中基于类的 View 使用 GET/POST 或特定方法吗?

python - Python 中无法解析 JSON 错误

python - 如何使用 DetailView 中 PK(主键)字段以外的不同字段进行查询

python - FK select 的显示选项 - Django Rest Framework

python - 覆盖和自定义 "django.contrib.auth.views.login"