I am missing something with Django's url paths - python

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),
]

Related

How do I change the URL in Django framework

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

Django: Page not found but path is shown

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.

how to read html file on Django

I started make a website for my school project, i use Django Frameworks
i want to make my homepage website, i already have html file, then what should i do to make Django read my homepage
enter image description here
this is my urls.py on main folder
urlpatterns = [
path('admin/', admin.site.urls),
path('katalog/', include('katalog.urls')),
path('', ) #what should i write here?
]
django doc: https://docs.djangoproject.com/en/3.0/topics/http/urls/#example, it offers the below example
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
You have to create in your apps views.py file a FBV or CBV, the easiest is a FBV like below:
def someview(request):
context={}#if you want to add objects to the front end
return render(request, 'yourhtmlfile', context)
and then import it in your urls.py:
from . import views
urlpatterns = [
path('home/', views.someview),

Navigation Links in Django

How to set proper navigation on navigation bar to go back to the homepage or to go into further section such as e.g "contact" or "prices"? Ill show quick example what I really mean:
Home -- Prices -- Contact
Being at the homepage I press "Home" button and of course it refreshes the site, because we are at the homepage. I press the Prices button and it goes into 127.0.0.1:8000/prices and everything seems to be okay
now we are in the prices section page -
Home -- Prices -- Contact
but now when i press "Contact" it goes to 127.0.0.1:8000/prices/contact, but I would like that to be 127.0.0.1:8000/contact, or by pressing now "home" it refreshes the site, its not going back into the homepage. Can you please give me tip what shall i do now?
my urls.py (whole project)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls') ),
]
my urls.py (application)
urlpatterns = [
path('', views.home, name="home-page"),
path('prices/', views.prices, name="prices-page"),
path('contact/',views.contact, name="contact-page"),
]
my views.py (application)
def home(request):
return render(request, 'home.html', {})
def prices(request):
return render(request, 'prices.html', {})
def contact(request):
return render(request, 'contact.html', {})
Add one more path, Like home/ in urlpatterns..
urlpatterns = [
path('', views.home, name="home-page"),
path('home/', views.home, name="home-page"),
path('prices/', views.prices, name="prices-page"),
path('contact/',views.contact, name="contact-page"),

django project url error

I created a project and created an application orders.
Views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'orders/index.htm')
def orders(request):
return render(request, 'orders/order.html')
urls.py:
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index.htm/', views.index, name='dashboard'),
url(r'^order.html/', views.orders, name='orders'),)
I used a ready to use site template.the link for 2 pages are:
index.htm index.htm is Dashboard
order.html order.html is Orders
when i go to link localhost:8000/orders/ , I click on orders the link looks like this.
and further click on two links in left pane multiple times, the resulting url look like this:
localhost:8000/orders/order.html/order.html
localhost:8000/orders/order.html/index.htm
I want to remove the order.html from middle so that it looks like:
localhost:8000/orders/index.htm
localhost:8000/orders/order.html
The links in your template do not have a leading slash, which means they are relative to the current directory.
If you are on the page with the URL http://localhost:8000/index.html/ and click on a hyperlink with href="order.html", it will take you to
http://localhost:8000/index.html/order.html
To fix it you must either
Add a slash to the links in your template, e.g. <a href="/order.html">
or drop the trailing slash from your URL definition in urls.py, e.g.
url(r'^index.htm', views.index, name='dashboard')
I suggest you use the template tag url in your templates instead of hard coding links.
If possible, I would also remove the .htm extension from both templates and urls.py
Change the following in urls.py file
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index/$', views.index, name='dashboard'),
url(r'^order/$', views.orders, name='orders'),)

Categories

Resources