Having problem in assigning urls to different views

31 views
Skip to first unread message

akshat

unread,
May 29, 2015, 6:05:47 AM5/29/15
to django...@googlegroups.com
I have a project - 'django_test'.django_test root folder contains one app - 'article',manage.py and django_test sub-folder. Inside django_test sub-folder I have urls.py -  

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    # Examples:
    # url(r'^$', 'django_test.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/$',include('article.urls')),
    url(r'^hello_template/$',include('article.urls')),
]


views.py inside article app looks like this - 

from django.shortcuts import render
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context

def hello(request):
name = "Hello World"
html = "<html><body> Hi,my name is %s</body></html>" %name
return HttpResponse(html)

def hello_template(request):
name = "Akshat"
t = get_template('hello.html')
html = t.render(Context({"name":name}))
return HttpResponse(html)

I want to give two urls like this for the above mentioned views - 'localhost:8000/hello/' and 'localhost:8000/hello_template'
urls.py inside article app right now looks like this - 

from django.conf.urls import url

from . import views

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

By this code If I give url - localhost:8000/hello,then it will look into django_test/urls.py and after finding r'^hello/$' it will go to article.urls.py and fetch the first url there which corresponds to hello class in views.py
I want to do the same steps for localhost:8000/hello_template url. But however after looking into django_test/urls.py and finding r'^hello_template/$' it will again go to article.urls.py and fetch the first url only which corresponds to hello class in views.py.How to point hello_template url to hello_template class in views.py.

Galia Ladiray

unread,
May 29, 2015, 7:02:02 AM5/29/15
to django...@googlegroups.com

You need the second URL to be /hello/template/
and then it the article.urls you add
url(r'^template/$, views.hello_template,name='hello_template')
Reply all
Reply to author
Forward
0 new messages