【问题标题】:django - url tag not workingdjango - url标签不起作用
【发布时间】:2013-05-21 01:51:37
【问题描述】:

我有这个表格:

<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 ??? %}

这个线程url template tag in django template 没有帮助我。

【问题讨论】:

  • 哪个版本的 Django?
  • 您希望在提交带有字段question == "如何制作面包"的表单后将用户重定向到neues_thema/how-to-make-bread/
  • @imkost,是的,几乎,带有字段标题。

标签: python django url


【解决方案1】:

你的 url 正则表达式需要一个参数,你的模板应该是这样的:

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

See url on Built-in template tags and filters docs

【讨论】:

  • 从 url 中删除参数:url(r'^neues_thema/$','home.views.c...
  • 但是,我想要这样的网址:neues_thema/blablablablabla
  • 模板与url一致。要生成neues_thema/blablablablabla,你应该在模板中写{% url create_question "blablablablabla" %}
  • 我可以动态给blbalablab吗?
  • @doniyor 你想要 url 中的参数并且不想使用参数来构建 url?
【解决方案2】:

你可以这样做:

url(r'^neues_thema/(?P<user>\w*)$','home.views.create_question',name="create_question"),

在你看来

def create_question(request, user=None):

【讨论】:

  • 我在想……确定这是一种避免 OP 错误的方法吗?我还没有测试过,但是,我认为当正则表达式为W* 时,django 也会丢失参数。你测试了吗?
  • 仍然需要参数
  • @danihp 它不会。这就是我们在末尾添加$ 的原因。
  • 我知道它不起作用 - 您是否遇到同样的错误?
  • 是的。同样的错误@karthikr。有没有正则表达式可以做到这一点:word_(\w*) 应该在视图中调用函数函数?
【解决方案3】:

这样写{% url 'home.views.create_question' alhphanumeric_work %}。它应该可以工作。

【讨论】:

  • alphanumeric_work 是您已传递给模板的变量。抱歉,在网址中没有看到 (w) 标记。您必须像我在上面所做的那样将字母数字代码作为变量或字符串
  • 不能动态地做到这一点。这就是现在的问题。
  • 你为什么不能动态地做到这一点。它是一个变量,所以只需动态设置该值。
【解决方案4】:

您的模板中似乎不需要{% 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"),

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})

带有用于创建问题的表单的模板:

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

回答你的“我做错了什么?”。您正在尝试通过掩码 neues_thema/(\w+)/ 呈现 url:{% url create_question %}。您的掩码需要一些参数((\w+)),但您没有输入任何参数。带参数渲染应该是{% url create_question title %}。但问题是:渲染页面时你不知道title

【讨论】:

  • 哇!!你说到点子上了!你找到了我的问题!谢谢谢谢
  • 我必须说:为什么我喜欢 django 是因为说 django 的人是如此可爱和诚实,无论遇到什么问题都愿意提供帮助。我以前从未见过这样的社区。我对 django/python 很满意,因为社区让我感到自信。
猜你喜欢
  • 2011-04-28
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 2020-05-02
  • 2014-02-16
  • 2016-03-22
相关资源
最近更新 更多