Django href link not routing to correct url - python-3.x

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.

Related

Issue with circular import in Django tutorials?

I am getting a circular import error with a basic Django page setup and I have no idea why. Am following the guides of at least 4 or 5 different tutorials with the same result.
I have varied the syntax in the webapps urls.py to no avail. Any ideas ?
mainproject
urls.py file
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('members/', include('members.urls')),
path('admin/', admin.site.urls),
]
webapp
Views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello world!")
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Error
"/Users/Desktop/website/venv/lib/python3.10/site-packages/django/urls/resolvers.py", line 706, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'members.urls' from '/Users/Desktop/website/main/members/urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

Django REST API don't see a URL

I think I have an import problem. After learning through all the tutorials and was able to get up to this point. I just want to see my database with .objects.all(). The problem is the code 404 where it can't see "receipts.url"
makemigrate and migrate is all done without any issues.
server was running with access to admin.
I am getting this problem, but I have it all mapped out. Please help and thanks again.
"Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/receipts/
Using the URLconf defined in project.urls, Django tried these URL patterns, in this order:
admin/
receipts/ [name='all']
The current path, receipts/, didn’t match any of these."
My project name is "project", my app name is "receipts". "project", "receipts" and "manage.py" all are in the same level.
I can see my database in the admin page, and as well as Mongodb compass.
Here are all of my files:
project/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'receipts',
'rest_framework',
]
project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('receipts/', include('receipts.urls')),
]
receipts/urls.py
from django.urls import path
from . import views
from .views import CustomersViews
urlpatterns = [
path(" ", CustomersViews.as_view(), name='all'),
receipts/views.py
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .serializers import CustomerSerializer
from .models import Customer
class CustomersViews(APIView):
def get_all(self, request):
customers = Customer.objects.all()
serializer = CutomerSerializer(customers, many=True)
return Response(serializer.data)
receipts/serializers.py
from rest_framework import serializers
from .models import Customer
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ('firstname',
'lastname',
'date',
'address',
)

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
]

Django not picking up the functions

I have tried every thing from making new function and saving every file thrice but nothing seems to work. And its my first time using Django if any one find anything please point it out...
Note: Django ver 3.1
Server Error:
path('', views.home_view),
AttributeError: module 'pages.views' has no attribute 'home_view'
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products', 'pages',
]
views.py
from django.http import HttpResponse
# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("Hello world")
urls.py
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home_view),
]
in urls.py change "from pages import views" to "from . import views"

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