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
Related
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
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
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' %}
I'm trying to import a simple single-field form in Django, and I have gone through plenty of Tutorial Videos on YouTube describing the same. Yet, I'm unable to render the simple form on my web-app. I'm pretty sure I'm doing something really stupid that is still oblivious to me.
I'll also post in the folder structure so you can suggest if I'm defining the class/function in the wrong views.py file.
The appropriate source codes are as follows:
earthquake/views.py File
from django.shortcuts import render
from earthquake.forms import HomeForm
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'earthquake/home.html'
def get(self, request, *args, **kwargs):
form1 = HomeForm()
argf = {
'myform': form1
}
return render(request, self.template_name, argf)
forms.py
from django import forms
class HomeForm(forms.Form):
post = forms.CharField()
home.html (snippet)
<div class="container">
<div class="jumbotron">
<h1>Query Form</h1>
<p>Please Enter the parameters you want to query against the USGS Earthquake DB</p>
<div class="container">
<form class="" method="post" action="">
{% csrf_token %}
{{ myform }}
<button type="submit" class="btn btn-success">Search</button>
</form>
</div>
</div>
</div>
Django Project urls (interview.py/urls.py)
from django.contrib import admin
from django.urls import path, include
from interview.views import login_redirect
from interview import views
from django.contrib.auth.views import LoginView
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('', login_redirect, name='login_redirect'),
path('admin/', admin.site.urls),
path('home/', include('earthquake.urls')),
path('login/', LoginView.as_view(template_name='earthquake/login.html'), name="login"),
path('logout/', LogoutView.as_view(template_name='earthquake/logout.html'), name="logout"),
path('register/', views.register, name='register'),
]
App URLS (interview/earthquake/urls.py)
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Folder Structure
https://i.stack.imgur.com/zoehT.jpg
(In case you're unable to see the last entry in the image, it's the main views.py present in the project folder).
The following is the snapshot of the render I currently get:
https://i.stack.imgur.com/kXR7W.jpg
I see that in your home views file your class based view is called
HomeView(TemplateView)
Yet in your app urls you are including the view as view.home when it should be
view.HomeView
to add to that, this is a classed based view so your urls page for that should look like:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home.as_view(), name='home'),
]
Since this is a class based view.
I'm trying to run a blog build with django on the browser. And I got this error:
NoReverseMatch at /
Reverse for 'blog.views.post_detail' not found.
'blog.views.post_detail' is not a valid view function or pattern name.
My url.py of my app looks like:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
]
It seems that when I type 127.0.0.1:8000/.
The url will direct to views.post_list.
And my views.py looks like:
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.filter(published_date__isnull=False)
return render(request, 'blog/post_list.html', {'posts': posts}
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
post_list() will render the request with post_list.html.
Inside post_list.html, the error comes from the line:
<h1>{{ post.title }}</h1>
I don't really understand what 'Reverse' means in the error message. 'blog.views.post_detail' does exist in views.py. I think I got everything needed for the code and can't figure out what went wrong.
I'm new to django, sorry if the question is basic and thanks for answering!
Django 1.10 removed the ability to reverse urls by the view's dotted import path. Instead, you need to name your url pattern and use that name to reverse the url:
urlpatterns = [
url(r'^$', views.post_list, name='post-list'),
url(r'^(?P<pk>\d+)/$', views.post_detail, name='post-detail'),
]
And in your template:
<h1>{{ post.title }}</h1>
It seems that your urls.py should be as follows:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^(?P<pk>\d+)/$', views.post_detail),
]
You should define a name for your url:
urlpatterns [
url(r'^$', views.post_list,name=post_list),
]
then use url tag like this:
AppName is you django application name.