我的项目结构是这样的:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Home/
templates/
Home/
Home.html
Login/
templates/
Login/
Login.html
Application/
templates/
Application/
Application.html
Details.html
mysite/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('Home.urls', namespace="Home")),
url(r'^Application', include('Application.urls',
namespace="Application")),
url(r'^Login', include('Login.urls', namespace="Login")),
url(r'^User', include('User.urls', namespace="User")),
]
首页/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.Home, name = 'Home'),
]
应用程序/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'Application', views.Application, name = 'Application'),
]
每个应用程序的views.py(s) 结构都是相同的(当然,名称有所变化)。
主页/views.py
from django.shortcuts import render
def Login(request):
return render(request, 'Login/Login.html')
我的 Home.html 中有两个链接。一个应该将用户引导至 Application.html,另一个则引导至 Login.html。我尝试执行以下操作:
模板/Home/Home.html
<a href="{% url 'Application:Application' %} ">Apply Now!</a>
<a href="{% url 'Application' %} ">Apply Now!</a>
<a href="/Login/Login.html">Login</a>
它们都不起作用。没有错误,没有重定向,什么都没有。 整个事情的奇怪之处是,尽管两个应用程序的结构相同,但登录和应用程序的 url 更改看起来不同:
网址更改为:
http://localhost:8000/ApplicationApplication.html
对比
http://localhost:8000/Login/Login.html 。
非常感谢任何帮助!
最佳答案
mysite/urls.py
url(r'^Application', include('Application.urls',
namespace="app")),
应用程序/Application.url
urlpatterns = [
url(r'Application', views.Application, name = 'application'),
]
home.html
<a href="{% url 'app:application' %} ">Apply Now!</a>
请更改重试
关于python - 如何在Django中引用不同应用程序中的不同模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900411/