Sukhrab
Sukhrab

Reputation: 95

How to refer to different templates in different apps in Django?

The structure of my project is this:

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")), 
]

Home/urls.py

from django.conf.urls import url, include
from . import views


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

Application/urls.py

from django.conf.urls import url, include
from . import views

urlpatterns = [
    url(r'Application', views.Application, name = 'Application'),
]

The structure for the views.py(s) is the same for each app (with changes in names, of course).

Home/views.py

from django.shortcuts import render

    def Login(request):
return render(request, 'Login/Login.html')

I have two links in my Home.html. One is supposed to direct a user to Application.html and the other, to Login.html. I tried to do the followings:

templates/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>

None of them work. No errors, no redirections, nothing. What is weird about this whole thing is that url changes for Login and Application look different, even though the structure of both apps are the same:

The url changes to:

http://localhost:8000/ApplicationApplication.html

vs

http://localhost:8000/Login/Login.html.

Would greatly appreciate any help!

Upvotes: 2

Views: 2732

Answers (1)

장진우
장진우

Reputation: 31

mysite / urls.py

url(r'^Application', include('Application.urls',    
    namespace="app")),

Application/Application.urls

urlpatterns = [
    url(r'Application', views.Application, name = 'application'),
]

home.html

<a href="{% url 'app:application' %} ">Apply Now!</a>

Please change to retry

Upvotes: 2

Related Questions