Friday, May 29, 2015

Re: Having problem in assigning urls to different views


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')


On Friday, May 29, 2015 at 12:05:47 PM UTC+2, akshat wrote:
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/c835367c-8a5b-47ee-86c6-159fc7ed457f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment