不同应用程序的Django URL映射

时间:2017-02-03 17:15:24

标签: html django

我试图将HTML中的按钮链接到我的django项目的项目文件夹中的另一个html。让我们说吧

MyApp

-Polls
   -templates
      -index
-Votes
   -templates
      -main
      -truths
      -rigged

我在main上有一个使用被操纵和真相的按钮,所以在主要它有这个按钮

<form action="{% url 'Votes:rigged' %}">
    <input type="submit" value="rigged votes" />
</form>

现在我想添加另一个将polls-&gt; index链接到它的按钮。有没有办法做到这一点,而不是将所有内容从民意调查复制到文件夹投票?

UPDATE * main.html中

<form action="{% url 'Polls:Index' %}">
    <input type="submit" value="Index" />
</form>

Polls.url

urlpatterns=[    
    url(r'^Index/', Index.as_view(), name="Index"),
]

Index.views

class IndexView(TemplateView):
    # template location
    template_name = "Polls/Index.html"

    # post logic must be defined
    def post(self, request, *args, **kwargs):
        return redirect(reverse_lazy("Polls:Index"))

1 个答案:

答案 0 :(得分:2)

请参阅https://docs.djangoproject.com/en/1.10/topics/http/urls/#url-namespaces 请理解命名空间和命名URL的工作方式。您的“MyApp”文件夹中应该有一个urls.py文件,其中包含“settings.py”和“wigs.py”等其他文件。 要通过名称空间引用url,您需要首先注册与Poll应用程序的url.py关联的名称空间。文档中的示例:

from django.conf.urls import include, url
urlpatterns = [
url(r'^polls/', include('polls.urls', namespace='Polls')),
]

此外,您还必须在'polls.urls'中为您的网址命名,如

url(r'^index/$', Whatever_view,name='index'),

然后你可以将网址称为“民意调查:索引”