Thursday, May 28, 2015

Having problem in assigning urls to different views

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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ae9d8ed2-6759-4bb6-885f-20b9fa66c5c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment