ERROR IN ADDING ANOTHER TEMPLATE IN EXISTING APP - django

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

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

How to fix 'The included URLconf 'gp.urls' does not appear to have any patterns in it'

when i tried to write the first urlpattern and the first view, i got this error, so i can be able to access to the authentification template, i have no idea what can be the source of this error
# my gp/urls.py
from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html')
when i try to run the server this is the error i get
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'gp.urls' does not appear to have any patterns in it. If you see valid
patterns in the file then the issue is probably caused by a circular
import.
this is my program tree
gp
apps
conge
organisation
personnel
stage
gp
pycache
init.py
settings.py
urls.py
views.py
wsgi.py
static
templates
gp
index.html
db.sqlte3
manage.py
# my gp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html', {})
what i edited is:
instead of from django.conf.urls import url i wrote from
django.urls import path
added {} in render function (its optional but just in case :) )
As stated in here: https://code.djangoproject.com/ticket/30728#no1
Maybe try to use latest Python version. Python 3.9 able to work

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

App error . Attribute error with first application

I have created this app in django but i cannot access the app . CMD says
Attribute error : module 'hello.views' has no attribute 'index'
VIEWS.PY
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World !!!')
URLS.PY/admin
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello.urls'))
]
URLS.PY/Hello
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.index, name='index')
]
You need to put commas after path().
path('hello/', views.index, name='index'), # Very important comma.
I think you missed one in each file.
I now see the other problem. Change your hello.urls.py path to
path('',
Your current configuration is looking for
/hello/hello/
If you put that in your browser it might work.

Django Project App Urls Page Not Found (404)

I still new to Django and I'm following this tutorial in YouTube. I can't proceed to the next tutorial because I hit an error.
When I type http://127.0.0.1:8000/music. I get this error.
Check my code below:
mysite urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/', include('music.urls')),
]
music urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
music views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello World</h1>")
Please help.
You might have not saved the project/urls.py file.
Or to check whether the music is set in INSTALLED_APPS in settings.py?