Optimal way to handle multiple link from url and views [duplicate] - django

I am trying to avoid self coping of views functions but have no idea ho to do it.
My views have a minor differences and there is definitely a way to
render html pages with a single function in this case. I experimented with urls "name" value but failed. I just started with django and hope that experienced programers have a solution. Thank you for your help.
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('team/', views.team, name='team'),
path('contacts/', views.contacts, name='contacts'),
path('researches/', views.researches, name='researches'),
path('publications/', views.publications, name='publications'),
]
and
views.py
def index(request):
return render(request, 'website/index.html')
def about(request):
return render(request, 'website/about.html')
def team(request):
return render(request, 'website/team.html')
def publications(request):
return render(request, 'website/publications.html')
def researches(request):
return render(request, 'website/researches.html')
def contacts(request):
return render(request, 'website/contacts.html')

You can capture a slug in the URL and use it to determine which template to render.
path('<slug:slug>', views.general_page, ...)
...
def general_page(request, slug):
return render(request, 'website/{}.html'.format(slug))

If your site is completely static, you may consider using a static framework, like Jekyll.
This way you don't have to depend on the host server's features, and avoid issues that may arise when you use a complex framework like django.

I would like to thank all people who tried to help me.
Your advises inspired me for a solution. I used a code below and it works good.
It appears that there is no need to create a separate view and you may use TemplateView for static html file rendering. That's exactly what I was looking for.
from django.contrib import admin
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='index.html'), name="index"),
path('about/', TemplateView.as_view(template_name='about.html'), name="about"),
path('team/', TemplateView.as_view(template_name='team.html'), name="team"),
path('contacts/', TemplateView.as_view(template_name='contacts.html'), name="contacts"),
path('researches/', TemplateView.as_view(template_name='researches.html'), name="researches"),
path('publications/', TemplateView.as_view(template_name='publications.html'), name="publications"),
]

This work for me i import the views.py in url.py.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('team/', views.team, name='team'),
path('contacts/', views.contacts, name='contacts'),
path('researches/', views.researches, name='researches'),
path('publications/', views.publications, name='publications'),
]
or
from layout.views import index,team,about ...
just import your views which folder is located, in this case i import views from the folder(app) layout

Related

URL not matching url pattern in Django

I'm trying to learn Django, went through the official tutorial and giving it a try on my own. I've created a new app and can access the index page but I can't use pattern matching to go to any other page. Here is my monthlyreport/url.py
from django.urls import path
from . import views
#app_name = 'monthlyreport'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
]
and my monthlyreport/views
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import generic
from .models import Report
def index(request):
report_list = Report.objects.all()[:5]
template = loader.get_template('monthlyreport/index.html')
context = {
'report_list': report_list,
}
return HttpResponse(template.render(context, request))
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
The debug for http://127.0.0.1:8000/monthlyreport/0 is showing
Using the URLconf defined in maxhelp.urls, Django tried these URL patterns, in this order:
monthlyreport [name='index']
monthlyreport <int:question_id>/ [name='detail']
polls/
admin/
accounts/
The current path, monthlyreport/0, didn’t match any of these.
Again, using http://127.0.0.1:8000/monthlyreport/ to go to index works fine, but I cant match the integer. I would really appreciate any suggestions, I am very, very confused at this point.
Problem with your code is of one slash(/). In your root urls.py file, from where you must have used something like:
path('monthlyreport', include('monthlyreport.url'))
So, the problem here is that you should have ending / in your path if you are setting your urls in monthlyreport.url like:
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
]
Or else, you have to put slash(/) infront of every new path like:
urlpatterns = [
path('', views.index, name='index'),
path('/<int:question_id>/', views.detail, name='detail'),
|
|
V
Here
]
Solution
In summary, convinient solution is adding slash(/) after path in urls.py file. Like:
path('monthlyreport/', include('monthlyreport.url'))
|
|
|
V
Add slash here

im getting error in django Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/about/

code of project named carparts urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('parts.urls')),
]
code for my app named parts urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('about', views.about,name='about'),
path('contact', views.contact,name='contact'),
]
code for views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request, 'index.html')
# return HttpResponse('this is home page')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
index page code output is show in http://127.0.0.1:8000 but my about and contact page shows Page not found (404) . Can any one help me with this code.
Thank you
Joint '/' in your both urls.py like that:
urlpatterns = [
path('', views.index,name='index'),
path('about/', views.about,name='about'),
path('contact/', views.contact,name='contact'),
]
According Django Design philosophy on Definitive URLs:
Technically, foo.com/bar and foo.com/bar/ are two different URLs, and
search-engine robots (and some Web traffic-analyzing tools) would
treat them as separate pages. Django should make an effort to
“normalize” URLs so that search-engine robots don’t get confused.

ERROR IN ADDING ANOTHER TEMPLATE IN EXISTING APP

Using the URLconf defined in rldjango.urls, Django tried these URL patterns, in this order:
[name='index']
<int:destinations_id [name='destionations']
admin/
accounts/
^media/(?P<path>.*)$
The current path, destinations.html, didn't match any of these.
***"***from main urls.py******
urlpatterns = [
path('', include('travello.urls')),
path('admin/', admin.site.urls),
path('accounts/',include('accounts.urls')),
]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
*from urls.py travello *
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
path('<int:destinations_id', views.destinations, name='destionations')
views.py of travello
from django.shortcuts import render
from .models import Destination
# Create your views here.
def index(request):
dests=Destination.objects.all()
return render(request,"destinations/index.html",{'dests':dests})
def destinations(request):
return render(request, "index.html")
please tell what i am doing wrong
i am a newbii
i am wanting to add one more template to my app index.html is added and i am trying to add destinations.html to it
i am taking tutorials from telesko django course
please help

Django: how to access functions in the views.py

I have a few functions in the view.py in my Django project:
Here is the views.py and urls.py under polls:
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def search(request):
return HttpResponse("You're at the polls search.")
polls/urls.py
from django.urls import path
from . import views
from django.conf.urls import include, url
urlpatterns = [
path('', views.index, name='index'),
path('', views.search, name='search'),
]
I am able to get the index page, but have trouble to reach the page in the search function. I got the error below:
How do I access the search function in the views.py? Thanks!
edit your polls/urls.py as below:
urlpatterns = [
path('', views.index, name='index'),
path('search/', views.search, name='search'),
]
the first argument of the path is the url pattern.
I think you misunderstood the third argument(name). it has nothing to do with the url pattern, it's a name for the url, that'll be useful for url reversing. read the document for more information

Page not found (404) Error in Django

My urls.py is
from django.conf.urls import patterns,url
from rango import views
urlpatterns=patterns('',url(r'^$',views.index,name='index'))
urlpatterns=patterns('',url(r'^about/$',views.about,name='about'))
My views.py is
from django.shortcuts import render
from rango.models import Category
# Create your views here.
from django.http import HttpResponse
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict={'categories':category_list}
return render(request, 'rango/index.html', context_dict)
def about(request):
return HttpResponse("go to index")
When I am trying to go to the address http://127.0.0.1:8000/rango I am getting page not found. But I am able to go to the address http://127.0.0.1:8000/rango/about.
When I remove the about url pattern in urls.py, I am able to go to the address http://127.0.0.1:8000/rango but not http://127.0.0.1:8000/rango/about, as the about url pattern does not exist.
I am unable to access both urls at once.
You have defined urlpatterns twice. The second patterns containing the about view replaces the first, which stops you accessing the index view.
Instead of,
urlpatterns=patterns('',url(r'^$',views.index,name='index'))
urlpatterns=patterns('',url(r'^about/$',views.about,name='about'))
it should be:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
)
In Django 1.7+, you don't need to use patterns any more, so you can simplify it to
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
]