django URLconf current path didn't match - python

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/ [name='index']
admin/
The current path, polls/, didn't match any of these.
My code is...
from (app)polls urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from (project)mysite urls.py
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
from views.py project file
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you are at the polls index.")

If you want your view index as a home view, the first page to be rendered on the address localhost:port/.
Then,
In polls.urls.py should be (the same as yours):
...
path('', views.index, name='index'),
...
And in project.urls.py
...
path('', include('polls.urls')),
...
It should work.
An extra.
Now if you want to render on the address, lets say, localhost:port/polls
Then your path pattern inside you app.url, (not project.url), would be:
path('polls/', polls_view, name='polls')
Hope it helps.

Since you are accessing localhost:8000, the search path is empty string '' and in your project urls, there are only two pattern for polls and admin. That's why you are getting the error. So you need to access localhost:8000/polls to get the result from index view of polls app.

I am a beginner and I have been troubled for a long time with the same problem.
The key to the problem is the directory:
mysite/
mysite/
polls/
....
I suspect that you have created a new file(urls.py) in mysite(The outermost layer of folders) directory and it's invalid. You should modify the file: mysite/mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]

Related

How to solve urls not found issue in django

I am building an app using django and notice that some new urls that i have created, the list is provided below are not being found.
I read somewhere that the slug might be the reason django cannot found the urls but i am not sure
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(config("ADMIN_URL"), admin.site.urls),
path("", include("core.urls", namespace="core")),
]
application urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
app_name = 'core'
urlpatterns = [
path('index/', views.index, name='index'),
path('home/', views.home, name='home'),
path('home/<slug:slug>/', views.user_lessons_details, name='user-lessons-details'),
path('home/join-transaction/', views.user_lessons_join_transaction, name='user-lessons-join-transaction'),
path('home/start-transaction/', views.new_test_epoint, name='new-test-epoint'),
path('home/draft/<slug:slug>/', views.user_lessons_core_action, name='user-lessons-draft'),
path('teachers/lessons/create/', views.merchant_lessons_create, name='teachers-lessons-create'),
path('teachers/lessons/list/', views.teachers_lessons_list, name='teachers-lessons-list'),
path('teachers/lessons/<slug:slug>/', views.teachers_lessons_details, name='teachers-lessons-details'),
path('teachers/lessons/<slug:slug>/update/', views.teachers_lessons_update, name='teachers-lessons-update'),
path('teachers/lessons/<slug:slug>/delete/', views.teachers_lessons_delete, name='teachers-lessons-delete'),
path('teachers/drafts/create/', views.teachers_core_actions_create, name='teachers-drafts-create'),
path('teachers/drafts/list/', views.teachers_core_actions_list, name='teachers-drafts-list'),
path('teachers/drafts/<slug:slug>/', views.teachers_core_actions_details, name='teachers-drafts-details'),
path('teachers/drafts/<slug:slug>/update/', views.teachers_core_actions_update, name='teachers-drafts-update'),
path('teachers/drafts/<slug:slug>/delete/', views.teachers_core_actions_delete, name='teachers-drafts-delete'),
path('teachers/home/list/', views.teachers_home_list, name='teachers-home-list'),
path('teachers/home/<slug:slug>/', views.teachers_home_details, name='teachers-home-details'),
path('teachers/home/create/course/', views.teachers_home_transaction_quote, name='teachers-home-create-course'),
path('teachers/home/create/something/', views.teachers_home_create_transaction, name='teachers-home-create-something'),
path('teachers/home/create/fields-selection/', views.teachers_home_fields_selection, name='teachers-home-fields-selection'),
# ALL THE NEW URLS BELOW return NOT FOUND in django no matter what i do
path('teachers/home/account/settings/', views.teachers_settings, name='teachers-settings'),
path('teachers/account/settings/update-password/', views.teachers_settings_update_password, name='teachers-settings-update-password'),
path('teachers/account/payments/', views.teachers_payments, name='teachers-payments'),
path('teachers/account/payments/update/', views.teachers_payments_update, name='teachers-payments-update'),
path('teachers/account/analytics', views.teachers_analytics_stuff, name='teachers-analytics-stuff')
]
How can i possibly solve this urls not found issue in django?
Please dont mind the urls meaning

How does Django know to call the index?

I just started learning Django and I am a bit confused. I'm following the official documentation and this is the code:
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls/views.py
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render
from django.views import View
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I am mainly confused that if I add more functions in polls/views.py, how will using inclde('polls.urls') know that I wish to call the index function or some other function?
Link for the documentation page that I am following: https://docs.djangoproject.com/en/3.2/intro/tutorial01/
Essentially it constructs a list of paths (strings) that it tries to build up and then find which you wanted
so paths in polls for "" and foobar would lead django to try to match up to the following urls
polls/
polls/foobar
The full path for the views.index is 'polls/' + '' and thus 'polls/', so it appends the two.
If you make an extra path:
# polls/urls.py
urlpatterns = [
path('', views.index, name='index')
path('foo/', views.other_view, name='other-view')
]
then that path pattern will be 'polls/'+'foo/' = 'polls/foo/', so it will disambiguate between the two.
If you thus visit polls/, then the first path will match and thus "fire" index. If you visit polls/foo, then the other-view will "fire".

Error message in Django when I try to runserver - does not appear to have any patterns in it. If you s ee valid patterns in the

Every time I write python manage.py run server. I get the error message:
The included URLconf '<module 'strana.views' from 'D:\\novi projekat\\strana\\views.py'>' 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.
mysite urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('strana/', include('strana.views')),
]
myapp(named strana)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Kita u Bila')
You included your views module. You want to include the module that contains your urls.
from django.contrib import admin from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('strana/', include('strana.urls')),
]

Django can't map url to view using include method

I have the following urls.py in my project dir,
Main project urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.render_calculator, name='render_calculator'),
path('calculator/', views.render_calculator, name='render_calculator'),
path('disclaimer/', views.render_disclaimer, name='render_disclaimer'),
path('cookiepolicy/', views.render_cookiepolicy, name='render_cookiepolicy'),
path('privacypolicy/', views.render_privacypolicy, name='render_privacypolicy'),
path('dashboard/', views.render_dashboard, name='render_dashboard'),
path('about/', views.render_about, name='render_about'),
path('admin/', admin.site.urls),
path(r'^', include('accounts.urls'))
]
Now I created a new app accounts (I added it to my apps in settings.py) where I would like to store the urls of that app in its own dir like so:
Accounts app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('register/', views.render_register, name='render_register'),
]
Accounts app views.py:
from django.shortcuts import render
def render_register(request, template="register.html"):
return render(request, template)
However, this configuration throws me this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/register
Using the URLconf defined in CFD.urls, Django tried these URL patterns, in this order:
[name='render_calculator']
calculator/ [name='render_calculator']
disclaimer/ [name='render_disclaimer']
cookiepolicy/ [name='render_cookiepolicy']
privacypolicy/ [name='render_privacypolicy']
dashboard/ [name='render_dashboard']
about/ [name='render_about']
admin/
^
The current path, register, didn't match any of these.
Where is the missing piece?
You are using path() with the regex r'^' which is causing your problem.
In order to define a path with a regex, you need to use re_path.
So change it to the following line:
re_path(r'^', include('accounts.urls'))
or you can use
path('', include('accounts.urls'))
Change this.
path(r'^', include('accounts.urls')) to
path('', include('accounts.urls'))

Django, url not found?

I'm trying out Python Django. With eclipse pyDev. But I'm unable to simply get my first url to display.
This urls.py is from the Cr package.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
This urlspy is from the Crowd package.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'Crowd/', include('Cr.urls'))
]
So what I've understood from this the Crowd package is the "main" webservice(?), and by using include I can whenever the regular expression matches Crowd, will pass it on to the other urls.py(Cr). But the debugger passes:
Using the URLconf defined in Crowd.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, crowd, didn't match any of these.
my views.py file
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
I tried to access it with http://127.0.0.1:8000/Crowd
Below is an image of the project folder.
Can we see your settings.py file? There is a place in there where you define your project's url file. I'm assuming it's right now either not there or it's pointing to the wrong place, because Django can't find your urls.py file.
For example, in my settings.py file for one of my projects, I have:
ROOT_URLCONF = 'Freya.urls'
Where "Freya" is my project name
Just for reference, not that I know this will solve your problem, this is what (part of) my urls.py file looks like for one of my projects:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'Crowd/', include('Cr.urls'))
]
just use this urls.py file

Categories