Django url page not found - python

So I have been trying to access a project post that I have created for my website, but every time I click on it the url can't be found. I am not sure why.
My code is below:
mywebsite/urls.py
from django.contrib import admin
from django.urls import re_path, include
from django.conf import settings
from django.conf.urls.static import static
#re_path(r'^admin/', admin.site.urls),
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('home.urls')),
re_path(r'^projects/', include('projects.urls', namespace="create_post")),
re_path(r'^contact/', include('contact.urls')),
]
projects/urls.py
from django.urls import re_path, include
from . import views
# urls for projects page
app_name = 'create_post'
urlpatterns = [
re_path(r'^$', views.retrieve_projects, name="retrieve_projects"),
#re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
re_path(r'^(?P<slug>[\w-]+)/$', views.details_of_project, name="details_of_project"),
re_path(r'^(?P<slug>[\w-]+)/update/$', views.update_projects, name="update_projects"),
re_path(r'^(?P<slug>[\w-]+)/delete/$', views.delete_projects, name="delete_projects"),
]
# To make images work
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
projects/views.py
# Function to retrieve all different projects
def retrieve_projects(request):
# Retrieves objects based on latest publish date
projects_list = Projects.objects.all().order_by("-publish_date")
context = {
'projects_list': projects_list,
}
return render(request, 'projects/projects.html', context)
projects/projects.html
<h1>asdasdasdasdasdasdas</h1>
{% if projects_list %}
<ul>
{% for project in projects_list %}
<h2>{{project.title}}</h2>
<h2>{{project.description}}</h2>
<h2>{{ project.publish_date }}</h2>
{% endfor %}
</ul>
{% endif %}
The error returns page not found

try this

Related

NoReverseMatch at /'blog' is not a registered namespace

I am trying to link two pages. Page files are home.html and pageOne.html. I am getting "NoReverseMatch at /'blog' is not a registered namespace". I am using django. When I first created the app, I named it artclBlog, I then created a templates folder and another folder within that one, this one I named blog. I think I should have kept these two names the same, this may have caused some confusion in my code.
pic of error
my views.py
from django.shortcuts import render, get_object_or_404
from .models import Blog
def home(request):
blogs = Blog.objects.order_by('-date')
return render(request, 'blog/home.html', {'blogs': blogs})
my urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from artclBlog import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('<int:blog_id>/', views.PageOne, name='PageOne')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my models.py
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=200, default='')
summary = models.CharField(max_length=200, default='')
pageOne = models.TextField(default='')
pageTwo = models.TextField(default='')
pageThree = models.TextField(default='')
pageFour = models.TextField(default='')
date = models.DateField(default='')
def __str__(self):
return self.title
home.html
{% for blog in blogs %}
<a href="{% url 'blog:PageOne' blog.id %}">
<h2 id="txt">{{ blog.title }}</h2>
</a>
<h4 id="txt">{{ blog.date|date:'M d y' }}</h4>
<p id="txt">{{ blog.summary|truncatechars:190 }}</p>
<hr>
{% endfor %}
You did not define an app_name in your urls.py, hence that means that blog: in blog:PageOne makes no sense. You thus either define an app_name, or remove the namespace.
Option 1: Add an app_name in urls.py
You can specify the namespace by writin an app_name in the urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from artclBlog import views
app_name = 'blog'
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('<int:blog_id>/', views.PageOne, name='PageOne')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This however means that thus all views now use this namespace, including home for example.
Option 2: Remove the namespace
Another option is to remove the namespace in the {% url … %} template tag [Django-doc]:
{% for blog in blogs %}
<a href="{% url 'PageOne' blog.id %}">
<h2 id="txt">{{ blog.title }}</h2>
</a>
<h4 id="txt">{{ blog.date|date:'M d y' }}</h4>
<p id="txt">{{ blog.summary|truncatechars:190 }}</p>
<hr>
{% endfor %}
This mistake comes usually when you have created different apps in your project.
First you do:
django-admin startproject mainapp
Then you do
python manage.py startapp secondapp
urls.py in your secondapp folder would automatically have app_name = 'secondapp'. However you must add in mainapp urls.py the following:
urlpatterns = [
...
path('secondapp/',include('secondapp.urls',namespace='secondapp')),
...
]
This is because your mainapp is the one that has the main access to the project and needs to know your secondapp urls.

Django : Problem with Django-allauth urls

I just started with a Django project using django-allauth, I configured the basic settings, without using any 3rd party provider. I have set up the urls.py of my project and urls.py of my app.
But on going to http://localhost:8000, I am getting to 'home.html' but how do I remove the navigation of allauth
The following is the urls.py of my project :
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('ckeditor/',include('ckeditor_uploader.urls')),
path('',include('blog.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
And this my urls.py of app :
from django.urls import path, include
from . import views
urlpatterns = [
path("", views.PostListView.as_view(), name = 'post_list'),
path("post/add", views.CreatePostView.as_view(), name = "create_new_post"),
]
views.py
from django.shortcuts import render
from django.views.generic import ListView, View
# Create your views here.
from .forms import PostForm, CommentForm
from .models import Post, Comment
class PostListView(ListView):
queryset = Post.objects.filter(is_published=True)
template_name = 'home.html'
class CreatePostView(View):
form_class = PostForm()
template_name = 'create_post.html'
model = Post
home.html
{% extends 'base.html' %}
{% block content %}
<h1>Hello World</h1>
{% for post in post_list %}
<h1>{{post.post_title}}</h1>
<p>{{post.post_body|safe}}</p>
{% endfor %}
{% endblock %}
path("post/add/", views.CreatePostView.as_view(), name = "create_new_post"),
add trailing slash to your url
your global urls.py:
path('',include('blog.urls')),
add something in your app urls.py:
path('test/',views.PostListView.as_view()),
after adding this to your urls.py, run your app again
the extended base.html file may contain the navigation. make changes there to remove or simply remove it
{% extends 'base.html' %}

Django shows page not found

I'm learning Chapter 18 18.4.2 in Python Crash Course,when i open http://localhost:8000/topics ,I'm using Django 3.0 and python 3.8
shows
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, topics, 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.
and this is my code
learning_log\urls.py
from django.contrib import admin
from django.urls import path
from learning_logs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name = 'index')
]
learning_logs\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name = 'index'),
url(r'^topics/$',views.topics,name='topics')
]
views.py
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
return render(request,'learning_logs/index.html')
def topics(request):
topics = Topic.objects.order_by('date_added')
context = {'topics':topics}
return render(request,'learning_logs/topics.html',context)
base.html
<p>
Learning Log-
Topics
</p>
{% block content %}{% endblock content %}
topics.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>{{ topic }}</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
and runserver shows:
Not Found: /topics
[06/Jan/2020 17:53:15] "GET /topics HTTP/1.1" 404 2077
enter image description here
First of all, good question. Love the detail.
The error is the following
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order
This means that the url /topics cannot be found. You need to import that url from the app specific urls.py into the main urls.py. This is usually done by something along the lines of
# in main urls.py
from learning_logs.urls import urlpatterns as ll_urlpatterns
# other patterns ...
urlpatterns += ll_urlpatterns
Use below code replace 'app' with your appname.
from django.contrib import admin
from django.urls import path
from learning_logs import views
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include(('app.urls', 'app'), namespace='app')),
]

loading images by list view in django template

I am trying to render by django template with photos saved in database by using listview so they can act like thumbnails like that of amazon.com but images are not loading
{% for offer in offer_details %}
{% if offer == None %}
<img src="{% static "pics/s7.jpg" %}" class="im">
{% else %}
<img src="{{offer.photo.url}}">
{% endif %}
{% endfor %}
views.py
class Index(ListView):
context_object_name = 'offer_details'
model = models.Offer_discription
template_name = "index.html"
from django.contrib import admin
from django.urls import path,include
from interface import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls, name="adi"),
path("",views.Index.as_view(), name="index"),
path("interface/",include("interface.urls")),
path("logout/",views.user_logout, name="logout"),
path("special", views.special,name="special"),
path("<int:pk>/", views.OfferDetailView.as_view(), name=" OfferDetailView")
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I was not adding that last line of code and last two imports

Django error during template rendering because URL tag

Error:
NoReverseMatch at /
Reverse for 'detail' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$(?P<college_id>[0-9]+)/$']
The error is on this line:
<li>{{ college.college_name }}</li>
Here is the whole template (index.html):
{% if latest_college_list %}
<ul>
{% for college in latest_college_list %}
<li>{{ college.college_name }}</li>
{% endfor %}
</ul>
{% else %}
<p> No colleges available </p>
{% endif %}
The view:
from django.shortcuts import get_object_or_404, render
from .models import College
# Create your views here.
def index(request):
latest_college_list = College.objects.order_by('college_name')
context = {'latest_college_list': latest_college_list}
return render(request, 'app/index.html', context)
def detail(request, college_id):
college = get_object_or_404(College, pk=college_id)
return render(request, 'app/detail.html', {'college':college})
urls.py:
from django.conf.urls import url
from . import views
app_name = "app"
urlpatterns = [
# campusarchitecture.com/
url(r'^$', views.index, name="index"),
# /college_name
url(r'^(?P<college_id>[0-9]+)/$', views.detail, name="detail")
]
root urls conf:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('app.urls')),
url(r'^login/$', include('login.urls')),
url(r'^admin/', admin.site.urls),
]
Anyone know what the problem is?
Here is the issue:
url(r'^$', include('app.urls')),
Should be
url(r'^', include('app.urls', namespace="app")),
Note the $ should be removed as it indicates the end of the regex pattern, and it would not discover the included url patterns.
Secondly, you need to explicitly specify the namespace in the include. More on this in the documentation here.
Similarly, remove the $ after the login/ URL pattern match too.

Categories

Resources