I got the error Page not found (404) when I try to reach the 'contact.html' and 'about.html' page
I don't understand why I'm getting this.
Here is my code:
app/urls.py:
urlpatterns = [
path('', views.frontpage, name="frontpage"),
path('<slug:slug>/', views.article, name="article"),
path('contact/', views.contact, name="contact"),
path('about/', views.about, name="about"), # If I write '/about/' that's working but the url is 'http://127.0.0.1:8000/%2Fabout/' and not 'http://127.0.0.1:8000/contact/' as i want. Same issue for contact
]
app/views.py:
def contact(request):
return render(request, 'contact.html')
def about(request):
return render(request, 'about.html')
# Of course the html file are created, they are located in the template folder
I'm trying to reach each page with this html code:
Home # This is working
Contact # This is not working
Aboout # This is not working
(citrouille is my app's name)
Here is the mysite/urls.py (root folder):
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('citrouille.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have others functions working perfectly such as the article page with the slug. But this two pages doesn't want to show. I'm pretty sure this is a stupid error but i can't point it.
Thank you for the help
Related
I am trying to render to different page using URLs in django,I have written all codes as the tutorials as well,
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name="index"),
path('', views.register,name="register"),
path('', views.login,name="login")
]
def index(request):
print("hello index")
return render(request,'index.html')
def register(request):
return render(request,'register.html')
def login(request):
print("hello login")
return render(request,'login.html')
but, whenever I hit that register link on my HTML it just always takes me to index.html.
<ul class="nav">
<li> Home</li>
<li> Register</li>
<li> Login</li>
</ul>
I have also tried putting something in the routes
urlpatterns = [
path('admin/', admin.site.urls),
path('index', views.index,name="index"),
path('register', views.register,name="register"),
path('login', views.login,name="login")
]
but this time it gives errors like The empty path didn’t match any of these.
I have worked on different django project using the above pattern and it was working perfectly fine.Where am I making it wrong? Any help would greatly be appreciated!
you have same path name so you need to give different path for different views.
for Example:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name="index"),
path('registe/', views.register,name="register"),
path('login/', views.login,name="login")
]
def index(request):
print("hello index")
return render(request,'index.html')
def register(request):
return render(request,'register.html')
def login(request):
print("hello login")
return render(request,'login.html')
I am very new to django. I have followed the steps from a video on youtube on how to create a simple blog. Basically, I have a portfolio main page that displays several things including the latest blogs. It all works well. I can see the latest posts on the main page. I can also click in each of them and they open up fine.
However, now, I want to create a new page (blog.html) that will hold all the posts. I created a blog.html and did the necessary settings in views.py and urls.py. However, for some reason I get an error when trying to access localhost/blog/
This is my urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name='work'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('blog/', views.blog, name='blog'),
]
Here views.py:
def home(request):
posts = Post.objects.all()
return render(request, 'home.html', {'posts': posts})
def post_detail(request, slug):
post = Post.objects.get(slug=slug)
return render(request, 'post_detail.html', {'post': post})
def blog(request):
posts = Post.objects.all()
return render(request, 'blog.html', {'posts': posts})
This happens if you trigger the post_detail view with a slug, and no Post with that slug exists. But here you simply trigger the wrong view. This is because <slug:slug>/ will also match blog/, so you will never trigger the blog/ view. You should reorder the views, so:
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name='work'),
# before <slug:slug>
path('blog/', views.blog, name='blog'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
]
It might however be better to make non-overlapping patterns, since now if you ever make a Post with blog as slug, that post is inacessible. You thus might want to rewrite the path to:
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name='work'),
path('blog/', views.blog, name='blog'),
# non-overlapping patterns
path('post/<slug:slug>/', views.post_detail, name='post_detail'),
]
Note: It is often better to use get_object_or_404(…) [Django-doc],
then to use .get(…) [Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using
.get(…) will result in a HTTP 500 Server Error.
I am trying to redirect an link from app to another page view function. But after pointing the page correctly I am getting NoReverseMatch Found error on apps main page which haves no connection to it.
This is urls.py of main Project
urls.py
urlpatterns = [
path('', views.home, name="home"),
path('admin/', admin.site.urls),
path('teacher/', include('teacher.urls')),
path('student/', include('student.urls')),
]
This is urls.py for respective app which is teacher
urls.py
urlpatterns = [
path('', views.index, name="index"),
path(r'^detailed/(?P<reportid>\d{0,4})/$', views.detailed, name="detailed"),
]
I am also including views.py as error is pointing at view.py
views.py
def index(request):
return render(request, 'teacher/report.html')
def detailed(request, reportid):
weeklyr = wreport.objects.all()
dailyr = dreport.objects.all()
split = SplitOfWeek.objects.all()
return render(request, 'teacher/detailed.html')
I have tried adding r'^teacher/$' at main urls.py and r'^$' at urls.py of teacher app but after adding it shows there is url found for teacher.
This is the detailed error message:
Reverse for 'detailed' with no arguments not found. 1 pattern(s) tried: ['teacher/\\^detailed/\\(\\?P(?P<reportid>[^/]+)\\\\d\\{0,4\\}\\)/\\$$']
You shouldn't use regexes with path
A simple fix would be to do:
urlpatterns = [
path('', views.index, name="index"),
path('detailed/<int:reportid>/', views.detailed, name="detailed"),
]
However this would allow any number for reportid. If you really to limit the length to four characters, then you could use the regex with re_path:
urlpatterns = [
path('', views.index, name="index"),
re_path(r'^detailed/(?P<reportid>\d{1,4})/$', views.detailed, name="detailed"),
]
Another option would be to register a custom path converter for reportid.
After help in understanding issue from #Alasdair i found that adding a default value for reportid in view function is solution.
so views.py changed to this
def detailed(request, reportid=None):
Also to go to that default value I added url for default view.
So urls.py changed to this
urlpatterns = [
path('', views.index, name="index"),
path('detailed/<int:reportid>/', views.detailed, name="detailed"),
path('detailed/', views.detailed, name="detailed"),
]
VERY new to Django and I am a little confused. I have placed a link in my html template file; however, clicking the link reports a 404 error.
App views.py file:
from django.shortcuts import render
def index(request):
return render(request, "login/index.html", None)
def terms(request):
return render(request, "login/terms.html", None)
App urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
path('', views.terms, name="terms")
]
Offending code in index.html:
By signing in, you agree to our Terms Of Use Policy
Clicking on the link pops a 404 error. Any help would be appreciated as I learn a new framework.
The first problem is that both the path to the views.index and views.terms share the same path. As a result, you have made views.terms inaccessible.
You thus should change one of the paths, for example:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('terms/', views.terms, name='terms')
]
You better use the {% url ... %} template tag [Django-doc] to resolve the URLs, since if you later decide to change the path of a certain view, you can still calculate the path.
In your template, you thus write:
By signing in, you agree to our Terms Of Use Policy
urlpatterns = [
path('', views.index, name="index"),
path('terms/', views.terms, name="terms")
]
By signing in, you agree to our Terms Of Use Policy
or
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')),
]