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
Related
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')
]
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/.
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'))
Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:
admin/
projects/
The empty path didn't match any of these.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("projects/", include("projects.urls")),
]
try like below.
path('',include('my_app.urls'))
hope it will solve your problem!
I am trying to do the following thing in urls.py but Django 2.0.5 doesn't seem to support url(). Instead of it, I used path() but still, its throwing invalid syntax error.
Can someone give a clearer picture of path() as it seems to be not supporting regex.
Providing the code here:
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('$', home_page)
path('admin/', admin.site.urls),
]
You miss a ,, and $ is unnecessary
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
Django2 has 2 functions for URLconfs, path(), re_path().
You can use regex paths (regular expression based paths) with re_path(), so remove $ and place , between two consecutive paths.
Note: Let suppose your app name is my_django_app created by python manage.py startapp my_django_app command.
I created a new Django app named my_django_app and tried, it works fine. I have the following code in my urls.py file.
"""my_django_proj URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from my_django_app.views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
References: https://docs.djangoproject.com/en/2.0/topics/http/urls/
django 2.0 - including urls file from an app to the root urls file
Thanks.
If you prefer to use url instead of path, that will work just fine. You just have to import from django.conf.urls instead. So your import statement should look like this:
from django.conf.urls import url
Django says on their documentation page that this feature will likely be deprecated in future versions to re-path, however, url still works fine for me, and I'm running Django 2.0.7... so, I imagine it would work with yours as well. I guess because of this, with Django version 2 and above, it nows decides when it creates the boilerplate project that instead of importing urls from django.conf.urls, it imports path from django.urls. (Note: PATH doesn't allow for regex)
What I typically do, is create an app specific urls.py. In that urls.py I'll import url from django.conf.urls and have my specific app level urls there:
from django.conf.urls import url
from app_name import views # have to import views
urlpatterns = [
url(r'^$', views.index),
url(r'^users$',views.users),
]
Then in the project level urls.py I'll add the include module as so I can link it to my app specific urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
from app_name import views
urlpatterns = [
url(r'^',include('app_name.urls')),
url(r'^admin/', admin.site.urls),
]
(Note: If you have a separate folder in between such that the folder structure looks something like mainproject>apps>app_name>(settings.py, views.py, admin.py etc...) you will have to create an __init__.py file in the apps folder as so Django can recognize the module.
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls)
]
As stated in the above answers, the $ is unnecessary while using path(). You are getting a syntax error due to the comma after admin.site.urls) which should be removed.