So I create an url in root/project/urls.py with this lines
from django.conf.urls import include
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('app.urls'))
]
while in my root/app/urls.py
from django.urls import path
from .views import UserView, AuthenticationView
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
So it is expected to give me http://localhost:8000/users/register and http://localhost:8000/users/auth urls.
Meanwhile my request doesn't behave as expected.
apparently it returns me a space between the root path and include path. I check my root/project/settings.py file I don't find any weird settings. Does anybody know what is going on?
The space is just for displaying how the URL is built up, on the debug screen only.
I have experienced the same and at first I too thought Django somehow added the space. In the end it really is that there is no match between the URL's specified and the URL in your browser. Safari doesn't display the full url, so error can happen quickly...
Some additional info on urls in Django can be found here.
The error message in your screenshot states that the request URL is http://localhost:8000/users does not exist.
Here you redirects /users/ to app.urls:
path('users/', include('app.urls'))
But in app.urls, you never included a pattern for when the URL ends with only "/users/". Instead, "/users/register/" and "/users/auth/" are both specified.
urlpatterns = [
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
So http://localhost:8000/users/register and http://localhost:8000/users/auth should be valid URLs, but http://localhost:8000/users is not.
You can add another URL pattern for when the URL ends at "/users/":
urlpatterns = [
path('', AuthenticationView.as_view()), # maybe the same as /auth/ ?
path('register/', UserView.as_view()),
path('auth/', AuthenticationView.as_view()),
]
In conclusion, Django's actually not wrong about that page does not exist (404), it's because you hadn't match http://localhost:8000/users in any of the urlpatterns.
Have you tried to use regular expressions?
path(r'^admin/', admin.site.urls)
Otherwise in the Django 2 version the urls schema has been changed, I use url function instead the path function, that can be a possible fix for your problem
Related
in django urls without a trailing slash in the end i get this result "Page not found 404"
the same project and the same code in one pc i get different result.
this code is when i get the page without slash:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
and this the same code but i should to add slash
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
]
what i am waiting in the frontend its my views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def about(request):
return HttpResponse('about page')
i am waiting for your help guys
This behaviour is controlled by the APPEND_SLASH setting. In the default setting APPEND_SLASH is set to True which means if the requested URL does not match any paths set in urls.py an HTTP redirect will issue to the same URL with a trailing slash.
Eg:
Assume a request to /foo.com/bar will redirect to /foo.com/bar/ in case the provided URL /foo.com/bar is an invalid URL pattern.
Documentation states that
Note that the redirect may cause any data submitted in a POST request to be lost.
The APPEND_SLASH setting is only used if CommonMiddleware is installed (see Middleware). See also PREPEND_WWW.
please read the documentation for further clarification.
So, I am not really lucky with latest django version tutorials, so I've had some problems with things that changed between some versions. One of this things is: althought I do exactly as I read/watch in the tutorials I always get the same result - all urls redirect to the same HTML page.
Here is my root urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('theblog.urls')),
]
Here is my app urls:
from django.conf.urls import url, include
from .views import HomeView, ArticleDetailView
urlpatterns = [
url('', HomeView.as_view(), name='home'),
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
]
For example, when I go to localhost:8000/articles/1 (or any other pk), it renders home.html (HomeView class) as if it was localhost:8000/.
Hope you can help me. Thanks!
There are two things wrong with your code.
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
This will not work. If you want to use url. you can't use <int:pk>, you need to use a RegEx:
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail")
Note that this will be deprecated in the future and if you are using django >=2.0 you should use path:
path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail")
However this will still direct you to the wrong view. django stops after the first URL pattern match.
Switch them around to fix that:
urlpatterns = [
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail"),
# path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail"), # alternative with path instead of url
url('', HomeView.as_view(), name='home')
]
It may be due to this line
url('', HomeView.as_view(), name='home'),
Because url wraps re_path there may be some logic which will treat the blank regex string as a wildcard. Try changing it to '/'
url('/', HomeView.as_view(), name='home'),
#code in 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'),
path('about/', views.about, name ='about'),
]
#code in views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
def about(request):
return HttpResponse("about harry")
Body
I am doing this while watching a tutorial, and the same thing is working for him. Moreover, if mistakenly I write the wrong code and run, cmd shows the errors and on reloading the browser the server doesn't work more. Then I need to restart CMD and again run manage.py. Please tell me the reason and also the solution, Thanks.
What errors do you get?
Besides there're 2 urls' files: url routes for pages are inside urls.py in the same folder where views.py are. In another urls.py (the one which is in the same folder with manage.py) you may need to write following, assuming that 'polls' is the name of you application:
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
]
According to Django docs: include() function allows referencing other URLconfs. Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
You should always use include() when you include other URL patterns. admin.site.urls is the only exception to this.
If you still get the error: check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/
I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page
For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.
Here is my code:
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('portal.urls')),
url(r'^login/', include('login.urls')),
]
login urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/', views.index, name="login"),
]
login views.py
from django.shortcuts import render
def index(request):
return render(request, 'login/login.html')
portal urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name="portal"),
]
I see 2 problems here:
As #DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.
You don't terminate the portal index URL, so it matches everything. It should be:
url(r'^$', views.index, name="portal"),
In addition, if the regex is login/$ but you enter http ://server/login, then it won't match wheras http://server/login/ will.
You could try changing the regex to login/*$, which will match any number (even zero) / on the end of the url.
So http: //server/login, http: //server/login/, http: //server/login//// would all match.
Or if you want to be specific, login/{0,1}$ might work (though that regex syntax is from memory!)
I followed the instructions in the tutorial
I made one change for debugging, here are the files:
mysite1\urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^abc/', include('polls.urls')),
url(r'^polls/', admin.site.urls),
url(r'^admin/', admin.site.urls),
]
File: mysite1\polls\urls.py
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^&', views.index, name='index'),
]
Now if I go to the site http://127.0.0.1:8000/polls/ then it shows the login page same as going to the site http://127.0.0.1:8000/admin/.
However, if I go to the site http://127.0.0.1:8000/abc/ it gives me the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/abc
Using the URLconf defined in mysite1.urls, Django tried these URL patterns, in this order:
^abc/
^polls/
^admin/
The current URL, abc, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Can someone guide me what I am doing wrong?
In mysite1\polls\urls.py It should be
url(r'^$', views.index, name='index'),
Notice that your code had an & instead of $. $ indicates a end-of-string match character.
I had a few issues with this, maybe it'll help someone. My VS code was running python 2.7 so the first line on page urls.py in the web project folder (not the polls folder) I had to change to "django.conf.urls import include, url" from "from "django.urls import include, path". I had to include .conf. and urls, path was not working and it was not working without "conf".
Another issue I had on this same page is I had to correct it to "urlpatterns = [url('', include('hello.urls'))..." from "urlpatterns = [path('', include("hello.urls"))". Path was not working so I got this to work.
The third thing is, Notice that there is a '' in the code. It should be blank otherwise when you go to url http://127.0.0.1:8000/ it doesn't redirect to views.py in the polls folder.
I hope this helps someone. Thanks