Django can't map url to view using include method - python

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

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

Link correctly to index.html in Python Django

I have a Python Django project with the following files.
proj1/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('website.urls')),
]
proj1/website/views.py
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {})
proj1/website/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
Under proj1/website/templates I have a few .html files.
How do I link to those in my .html files?
I have currently the following written in index.html
Home
I get error message when I press the link for the index.html though.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/index.html
Using the URLconf defined in leam.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, index.html, didn't match any of these.
What am I doing wrong? Do I need to specify where the index.html is located with a full path, or what is wrong here? The index.html loads with run manage.py.
Home
from django.urls import path
from . import views
app_name = 'website'
urlpatterns = [
path('', views.index, name="index"),
]
Try this, it should work.
The formula is {% url '<namespace or app name>:<view name>' %}

How to Change Django Default Page to your Design?

I uploaded my site to the live server, but still, I am seeing Django default page, I updated my urls.py file, but still, it's not working. and it's showing me this output. Please let me know where I am Mistaking.
I am trying to access this mydomain.com/dashboard
Using the URLconf defined in yoursite.urls, Django tried these URL patterns, in this order:
admin/
The current path, dashboard, didn't match any of these.
here is my urls.py file...
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^dashboard/', include('dashboard.urls')),
url(r'^accounts/', include('accounts.urls')),
url('', include('frontpanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my views.py file...
def index(request):
category = Category.objects.all().order_by('created_at')
return render(request, "base.html", {'category':category})
What version of Django are you using?
Try changing url to re_path (remember to import it first). I think url has been deprecated.
After Analyzing the question u never mentioned ur app urls.py you only mentioned project urls.py . As u have url(r'^dashboard/', include('dashboard.urls')), in ur project urls.py, there must be a file in your app also named urls.py which handles urls having prefix /dashboard/ , by default django doesnt make that file you need to manually make it add ur function names to redirect . For example this is my main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('display_report/', include("display_report.urls"))
]
then u have to make a file named urls.py in ur app also to handle the requests and redirect the the approaite functions , in my display_report app i made a urls.py that looks like this
from django.contrib import admin
from django.urls import path, include
from display_report import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [path('', views.index)]
And then it will redirect to function named index in ur views.py file inside ur app
from django.shortcuts import render, redirect, HttpResponse
# from .models import Employee, Tasks, Report, Fileupload, Fileuploadnext
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render(request."index.html")
Here my ur will be mydomain.com/display_report and my index.html file will be inside the template folder

django URLconf current path didn't match

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),
]

Django URL patterns not being read

So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))

Categories