Home > Net >  Django query is not reaching to the the page
Django query is not reaching to the the page

Time:09-25

I want to add a contact html page in my existing project. I put the html page in template where homepage is already residing. Now I have added a url for contact in project level directory which directing the query to app level url. This app level has a contact function in views. I have setup app level url as well but contact link isnt responding. In debug its showing a 200 ok status. Could somebody can point out to me what wrong i am doing??? This is project level url file

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('articles.urls')),
    path('contact', include('articles.urls')),
]

this is app level url file

urlpatterns = [
    path('', views.articles, name='articles'),
    path('', views.contact, name='contact'),
]

this is view function in which one function is returning article page but other one not responding

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

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

CodePudding user response:

In debug its showing a 200 ok status. Could somebody can point out to me what wrong i am doing?

You defined two paths with an empty string. They fire articles and contact respectively. Since articles is specified first, if you visit the "home page", it will fire the articles view.

Each HTTP request will only fire one view, and that view will produce an HTTP response. If you thus want to add something to contact somebody, then you should alter the articles view, or work with another path that will trigger the contact view.

CodePudding user response:

You cannot give 2 path for 2 views so you need you change your url path, you can do something like below if you have both article and contact view on articles app's views.py file.

project level urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('articles.urls')),
]

app level urls.py

urlpatterns = [
    path('article/', views.articles, name='articles'),
    path('contact/', views.contact, name='contact'),
]

Now you to use 127.0.0.1:800/article/ for article view and 127.0.0.1:800/contact/ for contact view.

  • Related