page not found when making new url / view Django - python

Hello i am trying to add a basic url called localhost:8000/shop
so that when i am on my homepage, I can click a link called shop and It will lead me to localhost:8000/shop
in my urls.py i added
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from homepage import views
urlpatterns = [
path('' , views.home),
path('admin/', admin.site.urls),
path('reviews/' , include('reviews.urls')),
path('shop/' , include('product.urls')),
]
in my folder called product i have a urls.py file with
from django.urls import include, path
from . import views
urlpatterns = [
path('shop/' , views.shop),
]
and in my product folder i have a views.py file with
from django.shortcuts import render
# Create your views here.
def shop(request):
return render(request, 'product/shop.html')
linking It to my html file inside my product folder..
when i run the server, I get this error message
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/shop
Using the URLconf defined in yorleico.urls, Django tried these URL patterns, in this order:
admin/
reviews/
shop/
The current path, shop, didn't match any of these.
What am i doing wrong?!

To register the path /shop, you need to use path('' , views.shop), in the urls.py in your shop app. The /shop prefix is already being defined by the path('shop/' , include('product.urls')), line in your project level urls.py.

All url patterns in the product app already start with shop/, due to the path('shop/', include('product.urls')). Therefore your urls.py for the products app should look like:
# product/urls.py
from django.urls import include, path
from . import views
urlpatterns = [
path('' , views.shop),
]
otherwise the path should be /shop/shop/.

Related

How to set url in Django?

I'm a newbie in web development and I'm learning to use Django. Unfortunately I have been stuck for more than 24 hours trying to figure out how to set the URL of a web page. I keep getting status 404 error displayed on the browser after running python server. I have checked python documentation and other documentations online but I still don't see anywhere I'm getting it wrong.
I have the following files in the main Django follow:
urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank', include ('qbank.url')),
path ('admin/', admin.site.urls),
]
settings.py
INSTALLED APPS = [
'qbank'
.....
]
In my project folder(which I named qbank) I have the following files:
urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('qbank'), views.index, name = 'index'
]
view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse ('Hello from Qbank')
The way you wrote it now, it will require two qbanks, one for the "root" urls.py, and one for the urls.py in the qbanks, hence localhost:8000/qbankqbank. If you only want to access it with qbank, then you remove the qbank for example from the urls.py of the qbanks app. So then the "root" urls.py looks like:
# project_name/urls.py
from django.contrib import admin
from django.urls import path, include
urlspatterns [
path('qbank/', include('qbank.url')),
path ('admin/', admin.site.urls),
]
and the urls.py of your app:
# qbank/urls.py
from django.urls import path
from . import views
urlspatterns = [
path ('', views.index, name='index')
]

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 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 URLs file error

I have the following urls.py file in a Django project, and I am getting an error which I assume is relating to the latest syntax relating to urls and paths.
The code I have in the urls file which is url.py in the mysite (outermost directory) is:
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path(r'^$', include('aboutme.urls')),
]
The error message is:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^$
The empty path didn't match any of these.
In the actual app (website folder) which is called 'aboutme', the urls.py file looks like this:
from django.urls import path
from django.conf.urls import url, include
from .import views #this is the main thing we are going to be doing ....returning views!
urlpatterns = [
path(r'^$', views.index,name='index'),
]
Can anyone shed any light on the correct syntax or what I am doing wrong?
UPDATE:
I also went back and tried to update the main mysite's url.py file to include the admin commands (which were in the previously working version). The code and resultant error are also shown below:
Tried this code:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
path(r' ', include('aboutme.urls')),
]
Error
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn't match any of these.
Remove the ^ and $ in the urls.py files.
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path(r'', include('aboutme.urls')),
]
And in your app urls.py:
from django.urls import path
from django.conf.urls import url, include
from .import views #this is the main thing we are going to be doing
app_name="myappname"
urlpatterns = [
path(r'', views.index,name='index'),
]
In django 2.0 they are not needed anymore if you are using path().
Related link: https://code.djangoproject.com/ticket/28691

Django 404: Page not found (404) - URLConf not matching

I'm new to Django, and my first major project of sorts is to go through an existing Django Web App to update it, streamline it and optimize it in any way that I can. I'm having some difficulty in getting it to run on my machine, however.
When navigating to the local development server at localhost:8000/ I get the following error:
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
^home/$ [name='portal']
^agentexoplanet/admin/
^agentexoplanet/agentex/ [name='agentex_redirect']
^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/(?P<calid>\d+)/$ [name='agentex_admin_calib']
^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/$ [name='agentex_all_calib']
^agentexoplanet/admin/
^agentexoplanet/
^static/(?P<path>.*)$
The current URL, , didn't match any of these.
This is the core.urls file:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles import views
from django.views.generic import RedirectView
from django.contrib.auth.views import login, logout
from agentex import urls
from agentex.views import home
from agentex.admin import calibrator_check, allcalibrators_check
#from admin.site import urls
#from showmestars.views import newimage, latestimages
admin.autodiscover()
urlpatterns = [
#(r'^api/', include('odin.api.urls')),
url(r'^home/$', home, name='portal'),
url(r'^agentexoplanet/admin/', include(admin.site.urls), name='agentexo_admin'),
url(r'^agentexoplanet/agentex/', RedirectView.as_view(url='/agentexoplanet/'), name='agentex_redirect'),
url(r'^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/(?P<calid>\d+)/$',calibrator_check, name='agentex_admin_calib'),
url(r'^agentexoplanet/admin/agentex/event/(?P<planetid>\d+)/calibrators/$',allcalibrators_check, name='agentex_all_calib'),
url(r'^agentexoplanet/admin/', include(admin.site.urls), name=''), # QUERY
url(r'^agentexoplanet/',include(admin.site.urls), name='agentexo_urls'),
#url(r'^showmestars/newimage/$', newimage, {'eventid':0}, name='showmestars_newimage'),
#url(r'^showmestars/(?P<eventid>\w+)/$', latestimages, name='showmestars_latestimage'),
#url(r'^showmestars/$', latestimages, {'eventid':0}, name='showmestars_latestimage_event'),
#url(r'^login/$',login, name='site_login'),
#url(r'^logout/$',logout, name='site_logout'),
]
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', views.serve),
]
And my project (agentex) urls file:
from django.conf.urls import include, url
from django.contrib.auth.views import login, logout
from .views import *
from django.conf import settings
urlpatterns = [
url(r'^$',index, name='index'),
url(r'^account/login/$', login, {'template_name' :'login.html'}, name='login'),
url(r'^account/logout/$', logout,{'template_name' :'logout.html'}, name='logout'),
url(r'^account/register/$', register, name='register'),
url(r'^account/$', editaccount, name='editaccount'),
url(r'^profile/$',profile, name='profile'),
url(r'^planets/$',target, name='target'),
url(r'^fitsanalyse',fitsanalyse, name='fitsanalyse'),
url(r'^test',tester, name='tester'),
url(r'^briefing/read/$',read_manual_check, name='read_manual_check'),
url(r'^briefing/$',briefing, name='briefing'),
url(r'^comment/$',addcomment, name='addcomment'),
url(r'^(?P<code>\w+)/view/$',addvalue, name='addvalue'),
url(r'^(?P<code>\w+)/graph/update/$',updatedataset, name='updatedataset'),
url(r'^(?P<code>\w+)/lightcurve/advanced/$',graphview, {'mode' : 'advanced','calid':None}, name='advanced-graph'),
url(r'^(?P<code>\w+)/lightcurve/me/$',graphview, {'mode' : 'simple','calid':None}, name='my-graph'),
url(r'^(?P<code>\w+)/lightcurve/calibrator/update/$',classifyupdate, name='classifyupdate'),
url(r'^(?P<code>\w+)/lightcurve/calibrator/$',graphview, {'mode' : 'ave','calid':None}, name='average-graph'),
url(r'^(?P<code>\w+)/lightcurve/calibrator/(?P<calid>\w+)/$',graphview, {'mode' : 'ave'}, name='calibrator-graph'),
url(r'^(?P<code>\w+)/lightcurve/$',graphsuper,name='super-graph'),
url(r'^(?P<code>\w+)/$',infoview, name='infoview'),
url(r'^(?P<code>\w+)/data.(?P<format>\w+)',measurementsummary, name='measurementsummary'),
]
I'm not entirely sure of the error here. My core.urls file links to index.html, but I imagine it isn't linking correctly. Does anybody have any ideas?
Thanks in advance (and apologies for not posting an image - StackOverflow wouldn't let me).

Categories