I'm trying to make a website using Django and I got some error message like this
Request URL: http://127.0.0.1:8000/store.html
Using the URLconf defined in greatkart.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
store/
^media/(?P<path>.*)$
The current path, store.html, didn't match any of these.
The problem is when I tried to click button it always print ./store.html not the ./store
it's my html code for the button
See all
And this is my Django for urls.py (main)
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('store/', include("store.urls"))
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
This urls.py(store)
urlpatterns = [
path('', views.store, name='store'),
path('<slug:category_slug>/', views.store, name='product_by_category'),
]
views.py
def store(request, category_slug = None):
return render(request, 'store/store.html', context)
Anyone have some idea? I want to fix it without changing the HTML code because I've tried it and when I click on the buttons twice that make some error because the URL print the ./store twice
Here the url should be absolute(starting with a / excluding you localhost address) and should not include .html.
So Your Url should be :
/store
and the anchor tag should be :
See all
Related
I was taking a youtube tutorial on making a basic website using django and I got this error when coding:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
app/
admin/
The empty path 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.
This is my code:
For urls.py/mysite:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
For views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world!")
For urls.py/myapp:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
You need to add root level path to access the path you specified http://127.0.0.1:8000/:
urls.py/mysite
urlpatterns = [
path('', views.index, name='home'),
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
views.py/mysite
def index(request):
return HttpResponse('This is home page')
Since you specified:
path('app/', include('myapp.urls')),
It means all the paths in myapp.urls are prefixed with app/. So you can access the index view with: http://127.0.0.1:8000/app/. Or if you want to access the index view with http://127.0.0.1:8000/ you rewrite the path to:
path('', include('myapp.urls')),
I have recently started using Django and I am a little lost as to why my services.html page does not render as I have followed what appears to be the same steps I took for my home page to work but I keep getting the error that nothing matches the current path. Below are my snippets for views and urls.py. I also had it be included in my website urls.py by using path('', include('affiliate.urls')),
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
[name='services']
The current path, services.html, didn't match any of these.
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.services, name="services"),
]
Views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html', {})
def services(request):
return render(request, 'services.html', {})
Change your urlpatterns as follows:
urlpatterns = [
path('', views.home, name="home"),
path('services/', views.services, name="services"),
]
Now when you want to access the services view, make sure to use 127.0.0.1:8000/services
because both of your paths for home and services are pointing to the same url.
Willem is trying to explain you the same thing.
I'm following this tutorial: https://www.youtube.com/watch?v=Z4D3M-NSN58
This is the point where I get the 404 error: https://www.youtube.com/watch?v=Z4D3M-NSN58&t=1044s
What I did so far:
Set up a new project,
Created a virtualenv,
Set up views.py,
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(response):
return HttpResponse("<h3>MySite</h3>")
Created urls.py inside my main folder,
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
Modified the urls.py inside project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
Upon connecting to http://127.0.0.1:8000/ I receive the 404 error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in project.urls, Django tried these URL patterns, in this order:
admin/
home/
The empty path didn't match any of these.
I am unable to proceed.
Solution:
urls.py inside the project folder has two paths, one for admin/ and one for home/.
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
Trying to connect to http://127.0.0.1:8000/home or http://127.0.0.1:8000/admin works, but leaving an empty path does not.
This is because if you type /home/ it goes inside the main/urls.py and looks for a /home/ path. The path is there and it displays what's inside the views.py. In order to display the desired output without entering a path at the end of the URL I had to change the /home/ path to an 'empty' path inside of the project/urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("main.urls")),
]
Now if no path is entered: http://127.0.0.1:8000/ It will go and look inside the main/urls.py for an empty path. It will find that an empty path leads to project/views.index and that is what it displays.
Add in your urls.py
urlpatterns = [
path("", views.index, name="index"),
]
and write a view to display message.
as you have put /home in your main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include("main.urls")),
]
so you will get a 404 error if you go to localhost:8000/
you have to go to localhost:8000/home to get the index page
I have an app in django that has a webpage with buttons to travel to another page via a menu and 8 hrefs. When I travel to the first page and try to click on another, I will encounter a 404 error.
Page not found (404)
http://127.0.0.1:8000/index.html/contact
Here is my url.py
urlpatterns = [
path('index.html/', views.homepage),
path('contact.html/', views.contact),
path('about.html/', views.about),
]
Views as well
def customers(request):
return render(request, 'customers.html')
def about(request):
return render(request, 'about.html')
def contact(request):
form_class = ContactForm
return render(request, 'contact.html', {
'form': form_class,
})
Settings is unchanged.
I believe that is all there is in making a URL path for a webpage.
I do not want http://127.0.0.1:8000/index.html/contact I want http://127.0.0.1:8000/index or http://127.0.0.1:8000/contact
How do I keep my URLs basic?
Your urls.py should contain the URL patterns. These can be different from the name of the files you put in your templates/ directory. As such, your patterns should not have ".html" extensions if you want to access without the ".html" in the URLs:
urlpatterns = [
path('index/', views.homepage),
path('contact/', views.contact),
path('about/', views.about),
]
When you click on some "Contact" link on the index page, you seem to build a URL relative to the index page, not the root of your site. In your templates, the href attribute of links should start with "/", e.g.:
Contact
instead of
Contact
Just remove .html from the urls.py
urlpatterns = [
path('index/', views.homepage),
path('contact/', views.contact),
path('about/', views.about),
]
I checked other pages to figure it out and I looked URL dispatcher but couldn't find a solution to this.
When I click to change the page from navbar nothing happens. homepage is extends to the header.html but others are not. infact when I click to another button change the page it stays still. here are my codes;
home/first/views.py
from django.shortcuts import render
def index(request):
return render(request, 'first/home.html')
def contact(request):
return render(request, 'first/iletisim.html', {'content':['Eger bizimle iletisime gecmek isterseniz mail adresimizi kullanabilirsiniz.', 'gulumhali#outlook.com']})
def home (request):
return render(request, 'first/home.html')
home/first/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]
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('first.urls')),
url(r'^blog/', include('blog.urls')),
]
SOLUTION:
Like Adam said below; if you having this problem change the order home/first/urls.py for views.index.
Your first URL is catching everything. Essentially it's saying if the URL has a start to it, send to index.
Since it's the first url in the list, it's always going to catch that first since it goes through the url patterns and finds the first one it matches. As you've found out in the comment, if you move it to the bottom, when you go to either home/ or contact/ it will catch those first.
However, you still will have a problem with that because if you go to asdfblahblah/ or anything, it's still going to catch that. What you should do is add a $ to show that there should be nothing in the URL to route to index. That regex says if there is a start, and an end, and nothing in between, route to index.
urlpatterns = [
url(r'^$', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]