Link correctly to index.html in Python Django - python

I have a Python Django project with the following files.
proj1/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('website.urls')),
]
proj1/website/views.py
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {})
proj1/website/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
Under proj1/website/templates I have a few .html files.
How do I link to those in my .html files?
I have currently the following written in index.html
Home
I get error message when I press the link for the index.html though.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/index.html
Using the URLconf defined in leam.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, index.html, didn't match any of these.
What am I doing wrong? Do I need to specify where the index.html is located with a full path, or what is wrong here? The index.html loads with run manage.py.

Home
from django.urls import path
from . import views
app_name = 'website'
urlpatterns = [
path('', views.index, name="index"),
]
Try this, it should work.
The formula is {% url '<namespace or app name>:<view name>' %}

Related

python: Django Page not found (404)

app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello', views.index, name='index')
]
project urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello, World!")
I've re-watched the tutorial several times and still can't find the error.
Since your project's urls.py includes hello using the prefix 'hello/' in
path('hello/', include("hello.urls"))
and your app's urls.py also has hello as the path in
path('hello', views.index, name='index')
you will need to access /hello/hello in your browser to get to a view that doesn't 404.
Django's technical 404 page (the one with the yellow background) should show the available paths -- if you don't get the technical 404 page, ensure you have the DEBUG setting turned on.

Django can't map url to view using include method

I have the following urls.py in my project dir,
Main project urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.render_calculator, name='render_calculator'),
path('calculator/', views.render_calculator, name='render_calculator'),
path('disclaimer/', views.render_disclaimer, name='render_disclaimer'),
path('cookiepolicy/', views.render_cookiepolicy, name='render_cookiepolicy'),
path('privacypolicy/', views.render_privacypolicy, name='render_privacypolicy'),
path('dashboard/', views.render_dashboard, name='render_dashboard'),
path('about/', views.render_about, name='render_about'),
path('admin/', admin.site.urls),
path(r'^', include('accounts.urls'))
]
Now I created a new app accounts (I added it to my apps in settings.py) where I would like to store the urls of that app in its own dir like so:
Accounts app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('register/', views.render_register, name='render_register'),
]
Accounts app views.py:
from django.shortcuts import render
def render_register(request, template="register.html"):
return render(request, template)
However, this configuration throws me this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/register
Using the URLconf defined in CFD.urls, Django tried these URL patterns, in this order:
[name='render_calculator']
calculator/ [name='render_calculator']
disclaimer/ [name='render_disclaimer']
cookiepolicy/ [name='render_cookiepolicy']
privacypolicy/ [name='render_privacypolicy']
dashboard/ [name='render_dashboard']
about/ [name='render_about']
admin/
^
The current path, register, didn't match any of these.
Where is the missing piece?
You are using path() with the regex r'^' which is causing your problem.
In order to define a path with a regex, you need to use re_path.
So change it to the following line:
re_path(r'^', include('accounts.urls'))
or you can use
path('', include('accounts.urls'))
Change this.
path(r'^', include('accounts.urls')) to
path('', include('accounts.urls'))

I'm going through a django tutorial but currently stucked in an error i.e.. "Page Not Found" on admin page

Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/
Using the URLconf defined in just.urls, Django tried these URL patterns, in this order:
1. admin/
2. shop/
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.
Here is my code:
Main ecom\urls.py:------------>>>>
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
]
Now shop\urls.py:-------------->>>>
from django.urls import path
from . import views
urlpatterns = [
path("",views.index, name="ShopHome")
]
And shop\views.py:-------------->>>>
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Shop Index")
Please help me guys
The only pages that should be working for you right now is /shop/ and /admin/. There is no root page because you have an include, which always start with /shop/ and checks the shop/urls. Not sure what you want, but if you want the root page to give you the index view, you can change ecom/urls shop url to:
path('', views.index, name="ShopName)
Dont forget to import the views of course
You have not defined blank URL. In main URLs.py, you have used path('shop/', include('shop.urls')), and in shop urls.py path("",views.index, name="ShopHome"). this path will redirect to http://127.0.0.1:8000/shop/ not http://127.0.0.1:8000/.
If you want blank url then do this:
ecom\urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('shop.urls'))
]
shop\urls.py
urlpatterns = [
path("",views.index, name="ShopHome")
]
remember whatever you add in path('', include('shop.urls')), it will be added in all URLs written inside shop app.
If you want a blank URL and also "shop" added in rest of the URLs, do this:
From shop import views
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
path('',views.index, name="ShopHome")
]

django URLconf current path didn't match

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/ [name='index']
admin/
The current path, polls/, didn't match any of these.
My code is...
from (app)polls urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from (project)mysite urls.py
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
from views.py project file
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you are at the polls index.")
If you want your view index as a home view, the first page to be rendered on the address localhost:port/.
Then,
In polls.urls.py should be (the same as yours):
...
path('', views.index, name='index'),
...
And in project.urls.py
...
path('', include('polls.urls')),
...
It should work.
An extra.
Now if you want to render on the address, lets say, localhost:port/polls
Then your path pattern inside you app.url, (not project.url), would be:
path('polls/', polls_view, name='polls')
Hope it helps.
Since you are accessing localhost:8000, the search path is empty string '' and in your project urls, there are only two pattern for polls and admin. That's why you are getting the error. So you need to access localhost:8000/polls to get the result from index view of polls app.
I am a beginner and I have been troubled for a long time with the same problem.
The key to the problem is the directory:
mysite/
mysite/
polls/
....
I suspect that you have created a new file(urls.py) in mysite(The outermost layer of folders) directory and it's invalid. You should modify the file: mysite/mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]

How to setup urls.py to work with app/urls.py and templates in src with Django

Trying to figure out how to setup my own project.
I created a new Django app to make a homepage.
src/home/urls.py:
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'views.index', name='index'),
]
src/home/views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "index.html", {})
src/project/urls.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', include('home.urls')),
url(r'^admin/', admin.site.urls),
]
src/templates/index.html:
<h1>Hello World</h1>
The reason this isn't in a templates folder inside of the home app is because I want to use this as my base template eventually for all apps and pages
Error reads:
ImportError at /
No module named 'views'
Using python 3.5 and django 1.9
EDIT*** changed to home.views.index
ERROR now reads:
TemplateDoesNotExist at /
index.html
In Site-wide Urls.py do the Following:
from app_name import urls as app_name_urls
from django.conf.urls import include
urlpatterns=[
path('',include(app_name_urls)
]
In App/urls.py
from django.urls import path
from .import views
urlpatterns=[
#your paths go here
]
Make sure you home is a package and you have __init__.py there.
You might also need to change views.index to home.views.index in your urls.py

Categories