Name parameter not recognized in Django - python-3.x

Everytime i get Page not found
i try renaming my views.py, I did my makemigrations and migrate, and nothing happened
Hi, I am working on this python project and I can't get the name parameters to work in my URLs. I have tried everything but my code is not recognizing them.
urlpatterns = [
path('estudiantes/', listar_estudiantes, name="listar_alumnos"),
path('profesores/', listar_profesores, name="listar_profesores"),
path('', inicio, name="inicio"),
path('admin/', admin.site.urls),
path('curso/', listar_curso, name="listar_curso"),
path('enviar-curso/', crear_curso, name="crear_curso"),
path('buscar-curso/', buscar_curso, name="buscar_curso"),
path('curso/<int:id>/', ver_curso, name="ver_curso"),
]

Related

Django href link not routing to correct url

The links within my pages/templates/base.html which are used for a header template results in 404 error. The pages load correctly when manually written 'http://127.0.0.1:8000' 'http://127.0.0.1:8000/about/'.
I am using class based views and following chapter 3 of Django for beginners (William Vincent).
pages/templates/base.html:
<header>
Home |
About
</header>
pages/urls.py:
from django.urls import path
from .views import HomePageView, AboutPageView
urlpatterns = [
path("", HomePageView.as_view(), name="home"),
path("about/", AboutPageView.as_view(), name="about"),
]
portfolio/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("pages.urls")),
]
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages.apps.PagesConfig',
]
pages/views.py:
from django.views.generic import TemplateView
# Create your views here.
from django.http import HttpResponse
class HomePageView(TemplateView):
template_name = 'home.html'
class AboutPageView(TemplateView):
template_name = "about.html"
The error is as follows:
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
about/ [name='about']
The current path, about/{% url 'about' %, didn’t match any of these.
Terminal error:
Not Found: /{% url 'about' %
[26/Oct/2022 11:40:02] "GET /%7B%%20url%20'about'%20% HTTP/1.1" 404 2497
I think the issue lies in pages/urls.py, as if click the 'home' link from 'http://127.0.0.1:8000/about/' the url is 'http://127.0.0.1:8000/about/%7B%%20url%20'home'%20%'. I've tried to remove the "about/" from " path("about/", AboutPageView.as_view(), name="about"),
"
I am also not sure whether the href tags have been written correctly. I have looked at this and this but can't figure it out. Thanks.
{% url 'about' % <- you need to close it properly.

Error 404 on Browser not showing Static folder file

When i imported os at first and write at the most bottom of settings.py
STATICFILES_DIRS =[
os.path.join(BASE_DIR,
'static')
]
To run my static folder which contains test.txt
But when I tried to run it on Opera it shows Page not foun 404.
Please tell what Can be the issue in this Django program.
You need to run the following command to collect static files first
python manage.py collectstatic
Your settings.py file must include
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))
After this you import static method in your urls.py file of project and then add
from django.conf.urls.static import static
urlpatterns = [ ]+
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Assign a simple HTML site with django

I want to assign/add a simple static HTML site to my django site. I know that's not what django is for but at the beginning I just want to create a simple site with a href to the other directories on the project.
For e.g.
index.html
<html>
<head></head>
<body>
anzeige<br />
Speiseplan <br />
Menu1<br />
Menu2<br />
Menu3<br />
</body>
</html>
Now I want to add this site to urls.py. I read that it shoud be possible with TemplateView.
I already tried this:
urlpatterns = [
path('', TemplateView.as_view(template_name="/templates/speiseleitsystem/index.html")),
path('speiseplan/', views.speiseplan_Anzeige, name='speiseplan_Anzeige'),
path('speiseplanEdit/', views.speiseplan_Eintragen, name='speiseplan_Eintragen'),
path('menu1/', views.speiseplan_menu1, name='speiseplan_menu1'),
path('menu2/', views.speiseplan_Anzeige, name='speiseplan_menu2'),
path('menu3/', views.speiseplan_Anzeige, name='speiseplan_menu3'),
]
My error code:
TemplateDoesNotExist at /
/templates/speiseleitsystem/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.6
Exception Type: TemplateDoesNotExist
Exception Value:
/templates/speiseleitsystem/index.html
Exception Location: C:\Users\xxxxx\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py, line 47, in select_template
Python Executable: C:\Users\xxxx\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.0
Python Path:
['C:\\Users\\xxxx\\Documents\\GitHub\\speiseplan\\speiseplan',
'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Thu, 18 Feb 2021 15:15:06 +0000
Did I missed something?
Depending on your project structure and settings, this might work:
In your url patterns, you have this line:
path('', TemplateView.as_view(template_name="/templates/speiseleitsystem/index.html")),
Change it to :
path('', TemplateView.as_view(template_name="speiseleitsystem/index.html")),
Django already knows that it has to look inside templates folder, given that your structure is something like this:
-project
- speiseleitsystem
\templates
\speiseleitsystem
\index.html
Remove /template/ from the url pattern and django will look in the correct place.
Currently it is looking for this directory:
project/speiseleitsystem/templates/speiseleitsystem/templates/speiseleitsystem/index.html
Which is surely not what you want it.
In case anyone will struggle with the same problem, the solution is a mix of all answers here.
Add the template directory into the setting.py inside TEMPLATES and there DIRS
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['speiseleitsystem/templates'],
'APP_DIRS': True,
'OPTIONS': {
],
},
},
]`
Note: Depending on the project structure only 'templates' is needed in DIRS
Add the right path to urls.py (as MoPo mentioned)
urlpatterns = [
path('', TemplateView.as_view(template_name="speiseleitsystem/index.html")),
]
Because of the template directory in setting.py templates/... is not needed here.

Error when adding multiple paths in django

When I add mulitple path in my code, it gives error.
Code
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello Nikhil")
def about(request):
return HttpResponse("About Nikhil")
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name='index')
path('about',views.about,name='about')
#error:Above line is giving error
]
when I run the code for 1 path,it does not give any error,
but when multiple path is added it givens error
urlpatterns is a list, so you need to add a comma after each path entry
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name='index'),
path('about',views.about,name='about'),
#error:Above line is giving error
]
#nikhil_2001
in your path
urls.py
path('',views.index,name='index')
the path doesn't have a comma at the end of it, you need to add a comma at the end of each path or the program won't work
path('about',views.about,name='about') in this line should be 'about/' and there should be a comma after each path so the correct code would be
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name='index'),
path('about/',views.about,name='about')
#error:Above line is giving error
]

Unresolved reference 'views' in django

urls.py
from django.contrib import admin
from django.urls import path, include
from crudapplication import views
urlpatterns = [
path('admin/', admin.site.urls),
path('emp', views.emp),
path('show', views.show),
path('edit/<int:id>', views.edit),
path('update/<int:id>', views.update),
path('delete/<int:id>', views.destroy),
]
ModuleNotFoundError: No module named 'webapplication.crudapplication'
program structure hierarchy
from `.`crudapplication import views
You forgot Dot(.)
simply because they are on different packages. Try this approach:
Define the urls.py in your crudapplication then use include in your main urls
In your crudapplication package, create urls.py with this:
from django.urls import path
from . import views
urlpatterns = [
path('emp/', views.emp),
# the rest of your views..
]
back to your webapplication's urls.py, insert following:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('crudapplication.urls'))
]
This will resolve the IDE error and solve the URLS routing.
Note that, your current approach will throw IDE warning but i believe ur routing still works.

Resources