Django: error trying to access detail view with re_path - python

To elaborate, I went down a bit of a rabbit hole just trying to make a trailing "/" optional in the URL. got it to sorta work with the project directory, but when on the blog app, I tried to use re_path in order to use regex to allow for an optional trailing "/" in the url, but I seem to get an error in the template
website urls.py (directory with settings.py)
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('blog/', include('blog.urls')),
path('blog', include('blog.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
blog urls.py
from django.urls import path, include, re_path
from .views import BlogView, ArticleView
urlpatterns = [
path('', BlogView.as_view(), name="blog"),
re_path(r'titles/(?P<slug_url>\d+)/?$', ArticleView, name="article")
]
blog.html template error appears at the href
{% extends 'base.html' %}
{% block content %}
<h1>Articles</h1>
{% for post in object_list %}
<h2>{{ post.title }}</h2>
{% endfor %}
{% endblock %}
blog views.py
from django.shortcuts import render, get_object_or_404, redirect
from .models import Post
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect
# Create your views here.
class BlogView(ListView):
model = Post
template_name = 'blog.html'
def ArticleView(request, url_slug):
post = get_object_or_404(Post, url_slug = url_slug)
return render(request, 'article.html', {'post':post})
Any help would be greatly appreciated, I just find the 404 I get without a trailing / to be very annoying

Related

django the print() is not working in the terminal

I am currently doing Django project with sqlite3 with ORM method.
I am unable to debug as print() is not working in the terminal even if I put print() function in views.py.
I checked in python shell, the queryset is working.
In views.py
from django.shortcuts import render,redirect
from .models import BookBoardModel
def index(request):
all_books = BookBoardModel.objects.all()
print(all_books)
for item in all_books:
print(item.title)
context = {'all_books': all_books}
return render(request, 'category_books_page/index.html', context)
The terminal shown with warning sign and not giving print():
Due to this, the variable all_books are not properly rendered in the index.html which will not generate any objects in index.html
In index.html
{{all_books}} It is not showing at all :(
In category_books_page.urls.py
from django.urls import path
from django.contrib import admin
from . import views
urlpatterns = [
path('', views.index, name='bookHome'),
]
In config.urls.py
from django.contrib import admin
from django.urls import path, include
from signup_page import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('default_page.urls')),
path('category_books_page/', include('category_books_page.urls')),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
You can try in template:
{% for book in all_books %}
{{ book.title }}
{% endfor %}
Try to restart runserver project

django objects method is not responding in views.py [duplicate]

I am currently doing Django project with sqlite3 with ORM method.
I am unable to debug as print() is not working in the terminal even if I put print() function in views.py.
I checked in python shell, the queryset is working.
In views.py
from django.shortcuts import render,redirect
from .models import BookBoardModel
def index(request):
all_books = BookBoardModel.objects.all()
print(all_books)
for item in all_books:
print(item.title)
context = {'all_books': all_books}
return render(request, 'category_books_page/index.html', context)
The terminal shown with warning sign and not giving print():
Due to this, the variable all_books are not properly rendered in the index.html which will not generate any objects in index.html
In index.html
{{all_books}} It is not showing at all :(
In category_books_page.urls.py
from django.urls import path
from django.contrib import admin
from . import views
urlpatterns = [
path('', views.index, name='bookHome'),
]
In config.urls.py
from django.contrib import admin
from django.urls import path, include
from signup_page import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('default_page.urls')),
path('category_books_page/', include('category_books_page.urls')),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
You can try in template:
{% for book in all_books %}
{{ book.title }}
{% endfor %}
Try to restart runserver project

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' %}

python - Django : Page Not found

I have looked around and can't really find a solution to my problem. Here is the error django throws. This error is being thrown when on my homepage I have a fiew links that upon clicking should direct you to a details view of said link.
Using the URLconf defined in untitled.urls, Django tried these URL patterns, in this order:
^$
^index/ ^$ [name='index']
^index/ ^(?P<distro_id>[0-9]+)/$ [name='distro_id']
^admin/
The current URL, index//, didn't match any of these.
To my knowledge I don't understand why this error is being thrown.
Here is my urls.py
from django.conf.urls import include, url
from django.contrib import admin
import index.views
urlpatterns = [
url(r'^$', index.views.index),
url(r'^index/', include('index.urls', namespace='index')),
url(r'^admin/', admin.site.urls),
]
My index/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# /index/
url(r'^$', views.index, name='index'),
#/distro/123/
url(r'^(?P<distro_id>[0-9]+)/$', views.detail, name='distro_id'),
]
My views.py
from django.shortcuts import get_object_or_404, render
from django.template import loader, RequestContext
from django.http import Http404
from .models import Distros
def index(request):
all_distros = Distros.objects.all()
context = {'all_distros': all_distros, }
return render(request, 'index/index.html', context)
def detail(request, distro_id,):
distro_id = get_object_or_404 (Distros, pk=distro_id)
return render(request, 'index/detail.html', {'distro_id': distro_id})
template code:
{% extends 'index/base.html' %}
{% block body %}
<ul>
{% for distro in all_distros %}
<li>{{ index.distro_id }}</li>
{% endfor %}
</ul>
{% endblock %}
I believe those are all the relevent files. I believe everything is setup correctly so I am not sure why the error is being thrown. I'm sure im missing something simple that i'm just overlooking.
Please don't use hardcoded URLs as they are error prone as in your situation. Instead of:
<a href="/index/{{ index.distro.id }}/">
use the url template tag with your namespace (index) and view name (distro_id):
<a href="{% url 'index:distro_id' index.id %}">
Note that you also have an error with index.distro.id as index is actually a Distros object. It has an id field, but not distro.id.

Python Django NoReverseMatch using URL in template

I know that this is a common issues, but none of the answers which I found here helped me out.
I cannot figure out what is wrong here (Yes I tried with '' and without them in url)
Here's what I got so far
template:
<html>
<body>
<div> Link here </div> {{ formText }}
</body>
</html>
url(own config)
from django.conf.urls import patterns, include, url
from metadaten import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
root url:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^metadaten/', include('metadaten.urls', namespace='metadaten')),
url(r'^admin/', include(admin.site.urls)),
)
views:
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.template import RequestContext, loader
from django.shortcuts import render, get_object_or_404
from metadaten.models import Title
from django.core.urlresolvers import reverse
def index(request):
return render(request, 'metadaten/index.html', {'formText' : 'foo'})
error message:
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found.
Any suggestions why I'm not able to build a simple href using {% url %} ?
please don't blame me if this question might be easy to figure out :(
You used a namespace with metadaten. You'll want to use {% url 'metadaten:index' %}.
Look at the last example for the url tag.

Categories

Resources