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'),
Related
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
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
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'm a new user of django and i have some difficulties.
I follow a Django 1.8 tutorial and i have Django1.11. the problem is about urls.py files & patterns
Here my code, blog/urls.py:
from django.conf.urls import url
from . import views
urlpattern = [
url(r'^accueil/$', views.home, name='home'),
]
first/urls.py:
from django.conf.urls import url include
urlpattern = [
url(r'^blog/', include('blog/urls')),
]
And when i use runserver command, it display :
URLconf '<module 'blog.urls'>'does not appear to have any patterns in it
I read somewhere that patterns are removed since 1.10 and it's an old syntax now but i don't found the solution.
Any idea ?
Django 1.11 is not released. You should use the actual released version, 1.10. Also, you should use the actual tutorial for your version.
However, the problem is that both of your files should define urlpatterns with an s, not urlpattern.
I think your need this page.URL dispatcher
Your code is fault, I think your need to change it:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^accueil/$', views.home, name='home'),
]
I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.