How can I add a model in custom CBV view? - python

My custom CBV is working fine but When I open a page it displays nothing.
All I want is when a get() is called it displays a Blog Post and form underneath the post and when form is filled it calls the post() and save it in the database. but when I open a blog post it displays nothing.
before moving to Custom CBV I was using the (detailView) everything was fine.
class DetailView(View):
form_class = CommentForm
template_name = "blogs/details.html"
model = BLOG
def get(self, request, slug):
forms = self.form_class(None)
blogs = self.model
context = {'forms': forms, "blogs":blogs}
return render(request, self.template_name, context)
def post(self, request):
forms = self.form_class(request.POST)
if forms.is_valid():
user_comment = forms.save(commit=False)
Name = cleaned_data["Name"]
Email = cleaned_data["Name"]
comment = cleaned_data['comment']
user_comment.save()
blogs/details.html
{% extends "blogs/main.html" %}
{% block dyn_title %}{{blog.blog_title}}{% endblock dyn_title %}
{% block body %}
<h2>{{blog.blog_title}}</h3>
<p> {{blog.blog_body|safe}} </p>
<form action="" method="post">
{% csrf_token %}
{% include "blogs/form_template.html" %}
</form>
{% endblock body %}
<!-- form_template.html -->
{% for field in form %}
<label>{{field.label_tag}}</label>
<div class="">
{{field}}
</div>
{% endfor %}

Related

No Reverse Match error for python crash course learning logs project

I am following the book Python Crash Course and am trying to do the Learning Logs app for Django. Everything was going well except when I tried to add entry forms for users on chapter 19. I am encountering the error
Reverse for 'new_entry' with arguments '('',)' not found.
1 pattern(s) tried: ['new_entry/(?P<topic_id>[0-9]+)/$']
My models.py look like this:
from django.db import models
# Create your models here.
class Topic(models.Model):
"""A topic the user is learning about"""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Entry(models.Model):
"""Something specific learned about a topic"""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""Return a string representation of the model."""
return self.text[:50] + "..."
My urls.py:
"""Defines url patterns for learning_logs"""
from django.urls import path
from . import views
urlpatterns = [
# Homepage
path('', views.index, name='index'),
path('topics/', views.topics, name='topics'),
#Detail page for single topic
path('topics/<int:topic_id>/', views.topic, name='topic'),
#page for adding a new topic
path('new_topic', views.new_topic, name='new_topic'),
#page for adding a new entry
path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
]
my views.py:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import Topic
from .forms import TopicForm, EntryForm
def index(request):
"""The homepage for learning Log"""
return render(request, 'learning_logs/index.html')
def topics(request):
"""show all topics"""
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
def topic(request, topic_id):
"""Show a single topic and all its entries"""
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('date_added')
context = {'topic':topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
def new_topic(request):
"""Add a new topic"""
if request.method != 'POST':
#No date submitted then create a blank form
form = TopicForm()
else:
# POST data submitted; process date
form = TopicForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('topics'))
context = {'form': form}
return render(request, 'learning_logs/new_topic.html', context)
def new_entry(request, topic_id):
"""add a new entry for a particular topic"""
topic = Topic.objects.get(id=topic_id)
if request.method != 'POST':
#no data submitted; create a blank form
form = EntryForm()
else:
# POST data submitted; process data
form = EntryForm(data=request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.topic = topic
new_entry.save()
return HttpResponseRedirect(reverse('topic', args=[topic_id]))
context = {'topic': topic, 'form': form, 'topic_id':topic_id}
return render(request, 'learning_logs/new_entry.html', context)
my new_entry.html:
{% extends "learning_logs/base.html" %}
{% block content %}
<p>{{ topic }}</p>
<p>Add a new entry:</p>
<form action="{% url 'new_entry' topic.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name='submit'>add entry</button>
</form>
{% endblock content %}
my base.html:
<p>
Learning Log -
Topics
</p>
{% block content %}{% endblock content %}
index.html:
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Learning Log helps you keep track of your learning, for any topic you're learning about. </p>
{% endblock content %}
my 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>
Add a new topic
{% endblock content %}
my new_topic.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>
Add a new topic
{% endblock content %}
my topic.html:
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topic: {{ topic }}</p>
<p>Entries:</p>
<p>
add new entry
</p>
<ul>
{% for entry in entries %}
<li>
<p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
<p>{{ entry.text|linebreaks }}</p>
</li>
{% empty %}
<li>
There are no entries for this topic yet.
</li>
{% endfor %}
</ul>
{% endblock content %}
I'm not sure what's going on. I DID read on the existing threads and followed some suggestions stating that I needed to add 'topic_id':topic on context or provide arguments, and so on, but nothing seems to work. If you would be kind enough to give me an idea of what's going on I would appreciate it.
In your topic.html you have:
add new entry
But you have never passed any topic_id in the context, although you have passed the topic so you can change it to:
add new entry
Try to change this line as:
<form action="{% url 'new_entry' topic_id=topic.id %}" method='post'>

Django crispy forms 'form-inline' doesn't works as expected

I have followed the crispy forms docs to create a simple inline form using bootstarp4. But it seems the form is not becoming a "inline" but a "horizontal".
Here is the code snippet that I have so far along with the screenshot of the rendered web view,
# models.py
from django.db import models
class InlineTestModel(models.Model):
name = models.CharField(max_length=20)
age = models.IntegerField()
email = models.EmailField()
# form_helper.py
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class FormHelperBase(FormHelper):
form_inputs = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_inputs()
def set_inputs(self):
for form_input in self.form_inputs:
self.add_input(form_input)
class TestInlineFormHelper(FormHelperBase):
form_class = 'form-inline'
field_template = 'bootstrap4/layout/inline_field.html'
layout = Layout(
'name', 'age', 'email'
)
form_inputs = [
Submit('Save', 'Save', css_class='button white')
]
# forms.py
from django import forms
class TestInlineForm(forms.ModelForm):
helper = TestInlineFormHelper()
class Meta:
model = InlineTestModel
fields = '__all__'
# views.py
from django.views import generic
from django.urls import reverse
class TestInlineAddView(generic.CreateView):
model = InlineTestModel
form_class = TestInlineForm
template_name = 'app_name/test_inline_add.html'
def get_success_url(self):
return reverse('app_name:success_url')
# template
{% extends 'expenses/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block form_body %}
<form method="post">{% csrf_token %}
{% crispy form %}
</form>
{% endblock %}
Rendered web view
The issue was, we don't need to use <form>...</form> tags while using the crispy tag
{% extends 'expenses/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block form_body %}
<form method="post">{% csrf_token %}
{% crispy form %}
</form>
{% endblock %}
In short, it needs just single line, as
{% extends 'expenses/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% crispy form %}

Reverse for 'edit_team' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['club_home/edit_team/(?P<pk>[0-9]+)/$']

Receiving the above error when trying to create a page to update my model using a serializer. Unable to use pk in edit_team view. Any ideas where I am going wrong? Delete button is working fine with the pk not sure why the update is not.
urls.py
path('teams/', views.TeamInfo.as_view(), name='teams'),
path('delete_team/<int:pk>/', views.delete_team, name='delete_team'),
path('edit_team/<int:pk>/', views.edit_team, name='edit_team'),
teams.html
<div class="team">
<h3>Team Details</h3>
<p>
{% csrf_token %}
{% for info in teams %}
<li>{{ info.club_id }}</li>
<li>{{ info.team_name }}</li>
<li>{{ info.manager_name }}</li>
<form action="{% url 'clubs:delete_team' info.pk %}">
<input type="submit" value="Delete">
</form>
<form action="{% url 'clubs:edit_team' info.pk %}">
<input type="submit" value="Edit">
edit_team.html
{% extends 'club_main_page.html' %}
{% load rest_framework %}
{% load staticfiles %}
{% block body_block %}
<div class="editteam">
<h3>Edit Team</h3>
<form action="{% url 'clubs:edit_team' pk=instance.pk %}" method="POST">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" name="" value="Update">
</form>
</div>
{% endblock %}
views.py
def delete_team(request, pk):
team = Team.objects.filter(pk=pk)
team.delete()
return redirect('clubs:teams')
def edit_team(request, pk):
instance = Team.objects.filter(pk=pk)
if request.method == 'POST':
serializer = TeamSerializer(request.POST, instance=instance)
if serializer.is_valid():
serializer.save()
return redirect('/')
else:
return redirect('/')
else:
serializer = TeamSerializer(instance=instance)
return render(request, 'edit_team.html', {'serializer': serializer})
Trackback: \clubkit\clubs\views.py in edit_pitch
return render(request, 'edit_pitch.html', {'serializer': serializer})
In your template you write:
<form action="{% url 'clubs:edit_team' pk=instance.pk %}" method="POST">
but there is no instance variable, you pass to the rendering process, you thus should add this one as well, so:
def edit_team(request, pk):
instance = Team.objects.filter(pk=pk)
if request.method == 'POST':
serializer = TeamSerializer(request.POST, instance=instance)
if serializer.is_valid():
serializer.save()
return redirect('/')
else:
return redirect('/')
else:
serializer = TeamSerializer(instance=instance)
return render(
request,
'edit_team.html',
{'serializer': serializer, 'instance': instance})
I'm not sure however, if redirecting to / is a good idea, certainly not if the serializer is not valid. In that case, one typically wants to render the page again, but with the errors. Therefore it is probably also a better idea to make use of forms [Django-doc].
You didn't pass the PK to the template, so it can't be used to generate the URL there. Change the last line of your view:
return render(request, 'edit_team.html', {''pk': pk, serializer': serializer})
Would have liken to figure this out using the serializer. But just went with a form here solution just in case any one needs it:
def edit_team(request, pk):
instance = Team.objects.filter(pk=pk).first()
if request.method == 'POST':
form = TeamForm(request.POST, instance=instance)
if form.is_valid():
form.save()
return redirect('clubs:teams')
else:
return redirect('clubs:teams')
else:
form = TeamForm(instance=instance)
return render(request, 'edit_team.html', {'form': form,
'instance': instance})
Then in the template:
{% extends 'club_main_page.html' %}
{% block head %}
<title>Edit Pitch</title>
{% endblock %}
{% block body_block %}
<div class="container">
<form method="post">
{% csrf_token %}
{{ form.as_p}}
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}

Django: can't access OneToOneField after rendering TemplateView Form

I am new to Django and don't understand what really is causing this:
I have a Model Company which has an OneToOneField, creator.
# models.py
class Company(models.Model):
class Meta:
verbose_name = 'Company'
verbose_name_plural = 'Companies'
creator = models.OneToOneField(User, related_name="company", on_delete=models.CASCADE, unique=False, null=True)
name = models.CharField(max_length=50)
I have a TemplateView class to handle get and post requests for creating a Company model:
# views.py
class create_company(TemplateView):
def get(self, request):
form = CompanyCreateForm()
title = "Some form"
return render(request, "form.html", {"form": form, "title": title})
def post(self, request):
form = CompanyCreateForm(request.POST)
if form.is_valid():
comp = form.save(commit=False)
comp.creator = request.user
comp.save()
return redirect('index')
The form is showing correctly also storing when I submit, the problem I am facing is with base.html where I show {% user.company %}; the form template extends it like:
{% extends "account/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<button class="btn btn-success" type="submit">Save</button>
</form>
<br>
</div>
<br>
{% endblock %}
and in base.html I access
{% if user.is_authenticated %}
{% user.company %}
{% endif %}
But user.company is not showing even it is set; it shows only when I redirect to index but not when I render the form.
Can someone help me understand what causes this?
{% if request.user.is_authenticated %}
{% request.user.company %}
{% endif %}
you are not sending any context to the base.html, thus only user wont work.
This was the error when I simulated your code.
Error during template rendering
In template /home/user/django/drf_tutorial/snippets/templates/base.html, error at line 2
Invalid block tag on line 2: 'user.company', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
1 {% if user.is_authenticated %}
2 {% user.company %}
3 {% endif %}
4 {% block content %}{% endblock %}
It gives hint that the code to show company should be variable {{ }} instead of tag {% %}. So the base.html template should be as below.
{% if user.is_authenticated %}
{{ user.company }}
{% endif %}
{% block content %}{% endblock %}

BootstrapError Parameter "form" should contain a valid Django Form

i have an app called reviews
reviews/forms.py
from django.forms import ModelForm, Textarea
from reviews.models import Review
class ReviewForm(ModelForm):
class Meta:
model = Review
fields = ['rating', 'comment']
widgets = {
'comment': Textarea(attrs={'cols': 40, 'rows': 15}),
}
reviews/views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Review, Wine
from .forms import ReviewForm
import datetime
from django.contrib.auth.decorators import login_required
#login_required
def add_review(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
form = ReviewForm(request.POST)
if form.is_valid():
rating = form.cleaned_data['rating']
comment = form.cleaned_data['comment']
user_name = form.cleaned_data['user_name']
user_name = request.user.username
review = Review()
review.wine = wine
review.user_name = user_name
review.rating = rating
review.comment = comment
review.pub_date = datetime.datetime.now()
review.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('reviews:wine_detail', args=(wine.id,)))
return render(request, 'reviews/wine_detail.html', {'wine': wine, 'form': form})
reviews/templates/reviews/wine_detail.html
{% extends 'base.html' %}
{% load bootstrap3 %}
{% block title %}
<h2>{{ wine.name }}</h2>
<h5>{{ wine.review_set.count }} reviews ({{ wine.average_rating | floatformat }} average rating)</h5>
{% endblock %}
{% block content %}
<h3>Recent reviews</h3>
{% if wine.review_set.all %}
<div class="row">
{% for review in wine.review_set.all %}
<div class="col-xs-6 col-lg-4">
<em>{{ review.comment }}</em>
<h6>Rated {{ review.rating }} of 5 by {{ review.user_name }}</h6>
<h5><a href="{% url 'reviews:review_detail' review.id %}">
Read more
</a></h5>
</div>
{% endfor %}
</div>
{% else %}
<p>No reviews for this wine yet</p>
{% endif %}
<h3>Add your review</h3>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'reviews:add_review' wine.id %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form layout='inline' %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Add
</button>
{% endbuttons %}
</form>
{% endblock %}
base.html
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
{% block bootstrap3_content %}
<div class="container">
<nav class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="{% url 'reviews:review_list' %}">Winerama</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>Wine list</li>
<li>Home</li>
</ul>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li>Hello {{ user.username }}</li>
<li>Logout</li>
{% else %}
<li>Login</li>
<li>Register</li>
{% endif %}
</ul>
</div>
</nav>
<h1>{% block title %}(no title){% endblock %}</h1>
{% bootstrap_messages %}
{% block content %}(no content){% endblock %}
</div>
{% endblock %}
I am getting the error at the line {% bootstrap_form form layout='inline' %} in the html file
Any idea how to fix this?
There's a few problems with your code as it stands, so I'll try to clean it up with some comments as I would write it to add a review to a wine.
#login_required
def add_review(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
if request.POST:
form = ReviewForm(request.POST)
else:
form = ReviewForm()
if form.is_valid():
### NO NEED FOR - already set as part of valid modelform ::: rating = form.cleaned_data['rating']
### AS WELL AS ::: comment = form.cleaned_data['comment']
### THIS IS NOT A FIELD IN YOUR FORM :::user_name = form.cleaned_data['user_name']
user_name = request.user.username
review = form.save(commit=False) # commit = False means that this instantiate but not save a Review model object
review.wine = wine
review.user_name = user_name # Why use this instead of a ForeignKey to user?
review.pub_date = datetime.datetime.now() # works as long as pub_date is a DateTimeField
review.save() # save to the DB now
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('reviews:wine_detail', args=(wine.id,))) # THIS will redirect only upon form save
return render(request, 'reviews/wine_detail.html', {'wine': wine, 'form': form})
Now, the error your seeing is most likely related to you passing request.POST to a form even if request.POST is blank; the form will attempt to set initial values but with a querydict that has no values that actually relates to the form.
EDIT: In response to your comments, my next step would be to try and render each form field individually and see if I can trigger a failure.
Instead of {% bootstrap_form form layout='inline' %}, try-
{% for field in form %}
{% bootstrap_field field %}
{% endfor %}
If this is an error with the django-bootstrap library trying to render the textarea widget and the inline style together (as I would suspect at this point), you can also eliminate the widget parameter and see if there's a fix. If there is, I'd suggest overriding your modelform's init method for assign a widget post a call super on init.
In Class Base View
This error may occur when you use form_class in the wrong generic view.
⮕ Open your views.py then check to see if you have set the wrong generic view in your class.
Example
class ProfileUpdateView(T̶e̶m̶p̶l̶a̶t̶e̶V̶i̶e̶w̶ UpdateView):
model = User
form_class = forms.ProfileForm
success_url = reverse_lazy("stories:story_list")
template_name = 'profile.html'
def get_object(self, queryset=None):
return get_object_or_404(User, pk=self.request.user.id)

Categories

Resources