Bootstrap 4 select form field isn't working with django - django

I am new to django and using bootstrap4 form with django. When I use input field text and date it works fine and save data into django admin but if I add Select for categories, it doesn't work and returns following error:
ValueError at /
The view app_budgetlist.views.home didn't return an HttpResponse object. It returned None instead.
I worked for 2 straight days and couldn't figure out what's wrong! Can you help please?
[N.B. Updated the code as #Caleb Goodman told still no luck]
Here is my code:
models.py
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class MonthlyBudget(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
budget_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
forms.py
from django import forms
from .models import MonthlyBudget
class MonthlyBudgetForm(forms.ModelForm):
class Meta:
model = MonthlyBudget
fields = ['category', 'budget_amount']
view.py
from django.shortcuts import render, redirect
from .models import Category, MonthlyBudget
from .forms import MonthlyBudgetForm
def home(request):
project = MonthlyBudget.objects.all()
categories = Category.objects.all()
if request.method == "POST":
form = MonthlyBudgetForm(request.POST or None)
if form.is_valid():
form.save()
return render(request, 'home.html', {'project':project, 'categories':categories})
else:
return render(request, 'home.html', {'project':project, 'categories':categories})
home.html
<form class="form-inline" method="POST">
{% csrf_token %}
<input type="text" name="budget_amount" class="form-control mb-2 mr-sm-2" id="budget-amount" placeholder="Amount">
<div class="form-group">
<label for="category">Select Category</label>
<select id="category" class="taskCategory" name="category">
<option class="disabled" value="">Choose a category</option>
{% for category in categories %}
<option class="" value="{{ category.name }}" name="{{ category.name }}">{{ category.name }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-primary mb-2">Add Project</button>
</form>
admin.py
from django.contrib import admin
from .models import MonthlyBudget, Category
admin.site.register(MonthlyBudget)
admin.site.register(Category)

You forgot to return a response after you save the form:
def home(request):
project = MonthlyBudget.objects.all()
categories = Category.objects.all()
if request.method == "POST":
form = MonthlyBudgetForm(request.POST or None)
if form.is_valid():
form.save()
return render(request, 'home.html', {'project':project, 'categories':categories})
else:
return render(request, 'home.html', {'project':project, 'categories':categories})

Related

I have created a django ModelForm that is not showing up in my html template, I am trying to determine why that code is not rendering my form?

models.py
from django.db import models
# Create your models here.
class Subscriber(models.Model):
"""A subscriber Model"""
email = models.CharField(max_length=255, blank=False, null=False, help_text="Subscriber Email Address", unique=True)
full_name = models.CharField(max_length=100, blank=False, null=False, help_text="First and Last Name")
class Meta:
verbose_name = "Subscriber"
verbose_name_plural = "Subscribers"
forms.py
from django.forms import ModelForm
from .models import Subscriber
class SubscriberForm(ModelForm):
class Meta:
model = Subscriber
fields = ["email", "full_name"]
views.py
from django.shortcuts import render
from .forms import SubscriberForm
from django.http import HttpResponseRedirect
from django.contrib import messages
# Create your views here.
def subscriber(request):
if request.method == "POST":
subscriber_form = SubscriberForm(request.POST or None)
if subscriber_form.is_valid():
subscriber_form.save()
messages.success(request, "")
return HttpResponseRedirect("/")
else:
subscriber_form = SubscriberForm()
context = {
"form_subscriber": subscriber_form
}
return render(request, "subscriber/subscriber_form.html", context)
subscriber_form.html
{% block content %}
<div>
<form method="POST">
{% csrf_token %}
{{ subscriber_form.as_ul }}
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
Only my submit button is publishing, however the form is never showing up for me.
I have followed the django docs exactly and still am not getting any good results.
It should be form_subscriber not subscriber_form so:
{% block content %}
<div>
<form method="POST">
{% csrf_token %}
{{ form_subscriber.as_ul }}
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
Additionally, I'd recommend you to only use SubscriberForm(request.POST) in views without using None for GET request as it is already being handled in else condition so:
views.py:
def subscriber(request):
if request.method == "POST":
subscriber_form = SubscriberForm(request.POST)
...

Can't save tags in django

i am using django-taggit to save tags. It save some tags but now its not working and idk why! still finding the solution. Please help me to find out what i am doing wrong. I am not getting any errors. But when i save the form using form.save_m2m() nothing is saved in my database. I have also checked my admin pannel. Nothing is storing in my db. Is there is any way to check this. I am also attaching the template.
views.py
from django.shortcuts import render,redirect,get_object_or_404
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from . models import UserCreatedNote
from django.template.defaultfilters import slugify
from . forms import AddNoteForm
from taggit.models import Tag
# Create your views here.
#login_required
def notes(request):
if request.method=='POST':
form = AddNoteForm(request.POST)
if form.is_valid():
form_data = form.save(commit=False)
form_data.user = request.user
key = form_data.note_title
form_data.slug = unique_slug_generator(slugify(key))
form_data.save()
#to save tags
form.save_m2m()
notes = UserCreatedNote.objects.filter(user=request.user)
form = AddNoteForm()
context = {'notes': notes,'add_note_form':form}
return render(request,'usernotes.html',context)
notes = UserCreatedNote.objects.filter(user=request.user)
form = AddNoteForm()
context = {'notes': notes,'add_note_form':form}
return render(request,'usernotes.html',context)
#login_required
def edit(request,slug):
note = get_object_or_404(UserCreatedNote, slug=slug)
tagsList = []
for tag in note.note_tags.all():
tagsList.append(tag.name)
print(tagsList)
if request.method == 'POST':
form = AddNoteForm(request.POST, instance=note)
if form.is_valid():
form_data = form.save(commit=False)
form_data.user = request.user
key = form_data.note_title
form_data.slug = unique_slug_generator(slugify(key))
form_data.save()
#to save tags
form.save_m2m()
form = AddNoteForm(instance=note)
context={'note':note,'u_form':form}
return render(request,'edit_note.html',context)
form = AddNoteForm(instance=note)
context={'note':note,'u_form':form}
return render(request,'edit_note.html',context)
models.py
from django.db import models
from django.contrib.auth import get_user_model
from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager
# Create your models here.
class UserCreatedNote(models.Model):
user = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)
note_title = models.CharField(default='',max_length=100,blank=True,null=True)
note_tags = TaggableManager()
note_data = RichTextField(default='',max_length=5000,blank=True,null=True)
slug = models.SlugField(unique=True,max_length=100)
creation_time = models.DateTimeField(auto_now_add=True)
last_modified_time = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-creation_time',]
def __str__(self):
return str(self.user)
template
<form action="" method="post">
{% csrf_token %}
<div class="modal-body">
<div class="row">
{{ add_note_form.media }}
{% for field in add_note_form %}
<div class="form-group col-lg-12 col-md-12">
{{ field | as_crispy_field }}
</div>
{% endfor %}
<div class="form-group col-lg-12 col-md-12">
<label for="tags">Note Tags</label>
<input type="text" id="tags" data-role="tagsinput" class="form-control" name="note_tags">
</div>
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<input type="submit" class="btn btn-success" value="Add">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>

Django: Including a form inside a listview

I am trying to insert a newsletter signup form into my base.html template which is a listview that displays upcoming events and featured shops and everytime I submit the form it returns a 'HTTP error 405'
Any help with this would be appreciated
Views.py
from django.shortcuts import render
from django.views.generic import ListView, TemplateView
from events.models import Event
from newsletter.forms import NewsletterSignUpForm
from shops.models import Shop
class HomeListView(ListView):
template_name = 'core/index.html'
def get_context_data(self, **kwargs):
context = super(HomeListView, self).get_context_data(**kwargs)
context.update({
'events': Event.get_upcoming_events()[:1], # returns only the first event in the list
'shops': Shop.objects.all(),
})
context['newsletter_form'] = NewsletterSignUpForm()
return context
def get_queryset(self):
return None
forms.py
from django.forms import ModelForm
from .models import Newsletter
class NewsletterSignUpForm(ModelForm):
class Meta:
model = Newsletter
fields = ['email']
Models.py
from django.db import models
class Newsletter(models.Model):
email = models.EmailField(unique=True)
date_subscribed = models.DateTimeField(auto_now=False, auto_now_add=True)
def __str__(self):
return f'{self.email}'
base.html
<form method="post">
{% csrf_token %}
{{ newsletter_form|crispy }}
<button class="btn btn-primary" type="submit">Sign Up!</button>
</form>
first add action url in form to handle post data
<form method="post" action="{% url 'submit_url' %}">
{% csrf_token %}
{{ newsletter_form|crispy }}
<button class="btn btn-primary" type="submit">Sign Up!</button>
</form>
urls.py
add url
path('your_url',views.formSubmit,name='submit_url')
views.py
def formSubmit(request):
if request.method == 'POST':
form = NewsletterSignUpForm(request.POST)
if form.is_valid():
form.save()
return redirect('your_list_view_url')
or you can use FormMixin along with classbased views
formmixin with classbased views

Django: How to save data to ManyToManyField

Your help will be nice for me. Here are that codes:
models.py:
from django.db import models
class TagModel(models.Model):
tag = models.CharField(max_length=50)
def __str__(self):
return self.tag
class MyModel(models.Model):
title = models.CharField(max_length=50)
tag = models.ManyToManyField(TagModel)
forms.py:
from django import forms
from .models import *
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = '__all__'
views.py:
from django.shortcuts import render, get_object_or_404, redirect
from .models import *
from .forms import *
def MyWriteView(request):
if request.method == "POST":
mywriteform = MyForm(request.POST)
if mywriteform.is_valid():
confirmform = mywriteform.save(commit=False)
confirmform.save()
return redirect('MyDetail', pk=confirmform.pk)
else:
mywriteform = MyForm()
return render(request, 'form.html', {'mywriteform': mywriteform})
form.html(1st trial):
<form method="post">
{% csrf_token %}
{{ mywriteform }}
<button type="submit">Save</button>
</form>
form.html(2nd trial):
<form method="post">
{% csrf_token %}
{{ mywriteform.title }}
<select name="tags" required="" id="id_tags" multiple="">
{% for taglist in mywriteform.tags %}
<option value="{{taglist.id}}">{{taglist}}</option>
{% endfor %}
</select>
<button type="submit">Save</button>
</form>
I am trying to add tags on my post. I made a simple manytomany tagging blog but it does not work. I submitted a post by clicking the save button, and the title was saved, but the tag was not. In the admin, it worked well.
Thank you in advance.
update the code like this
if mywriteform.is_valid():
confirmform = mywriteform.save(commit=False)
confirmform.save()
mywriteform.save_m2m()
return redirect('MyDetail', pk=confirmform.pk)
for more details Refer here

how to submit a form and formset at the same time

I am trying to render a form and a formset at once.
The formset is working fine (i think), but the form is not validating (as if there was no data being posted)
i have tried playing with the button but its main submit function comes through js.
the forms all work independently but not when submitted togetehr so it seem like the problem is in the views
here is the code:
views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from forms import LessonForm, AddMaterialForm
from models import Lesson, SUBJECT_OPTIONS, Materials, MATERIAL_TYPES
from django.forms.formsets import formset_factory
def Create_Lesson(request):
AddMaterials=formset_factory(AddMaterialForm, extra=9)
if request.method == "POST": # If the form has been submitted...
lesson = LessonForm(request.POST, prefix="lesson") # A form bound to the POST data
formset = AddMaterials(request.POST, request.FILES) # A form bound to the POST data
if lesson.is_valid() and formset.is_valid(): # All validation rules pass
lesson = lesson.save(commit=False)
lesson.creator = request.user
lesson.save()
for form in formset:
form = form.save(commit=False)
form.lesson = lesson.pk
form.save()
return render(request, 'index.html',)
else:
lesson= LessonForm(prefix='lesson') # An unbound form
formset = AddMaterials()
return render(request, 'create_lesson/create.html', {
'form': lesson,'formset':formset
})
.html
<form id="create_lesson_form" method="post" action="">
<h2>1: Create Your Lesson</h2>
{{ form.non_field_errors }}
<label for="subject"><span>Subject</span></label>
{{form.subject}}
{{ form.subject.errors }}
<label for="title"><span>Title</span></label>
<input type="text" id="title" name="name" placeholder="Give it a name"/>
{{ form.name.errors }}
<label class="error" for="title" id="title_error">You must choose a title!</label>
<label for="subtitle"><span>Subtitle</span></label>
<input type="text" id="subtitle" name="subtitle" placeholder="Type your subtitle here"/>
{{ form.subtitle.errors }}
<label class="error" for="subtitle" id="subtitle_error">are you sure you want to leave subtititle blank?</label>
<label for="description"><span>Description</span></label>
<textarea id="description" name= "description" cols="42" rows="5" placeholder="why is it important? this can be a longer description"'></textarea>
{{ form.description.errors }}
<label class="error" for="description" id="description_error">are you sure you want to leave the description blank?</label>
<label for="success" id="Goals_title"><span>Goals</span></label>
<textarea id="success" name="success" cols="42" rows="5" placeholder="explain what sucess might look like for someone doing this lesson...what does mastery look like?" '></textarea>
{{ form.success.errors }}
<label class="error" for="success" id="success_error">are you sure you want to leave the goals blank?</label>
{{ form.directions.errors }}
<label class="error" for="directions" id="directions_error">are you sure you do not want to include dierections?</label>
<label for="directions" id="Directions_title"><span>Directions</span></label>
<textarea id="directions" name="directions" cols="42" rows="5" placeholder="you can add simple directions here" '></textarea><br>
</form>
<form id="add_elements_form" method="post" action="">
{% csrf_token %}
{{ formset.as_p}}
<button type='submit' id='finish'>Finish Editing Lesson</button>
</form>
This will submit the form and the formset at the same time.
//When your uploading files or images don't forget to put "multipart/form-data"
// in your form.
//To connect formset in your form, don't forget to put the model in the formset
// for instance.
//In this you can add many lines as you want or delete it.
forms.py
class LessonForm(forms.ModelForm):
class Meta:
model = Lesson
MaterialsFormset = inlineformset_factory(Lesson, Materials,
fields=('field_name', 'field_name'), can_delete=True)
views.py
def view_name(request):
form = LessonForm()
formset = MaterialsFormset(instance=Lesson())
if request.method == 'POST':
form = LessonForm(request.POST)
if form.is_valid():
lesson = form.save()
formset = MaterialsFormset(request.POST, request.FILES,
instance=lesson)
if formset.is_valid():
formset.save()
return render(request, 'index.html',)
return render(request, "page.html", {
'form': form, 'formset': formset
})
html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
{{ formset.as_p }}
<input type="submit" value="Save"/>
</form>
You only need one form tag. If you expect to receive all of the data at the same time, you need to wrap all of the fields with one form tag.
Now that django 4 is out it's possible to do the same thing within the form itself using the Reusable templates. I prefer this solution because it is then simpler to reuse complex forms without messing with the views.
For the record, here is how I do it
The 2 related models:
# models.py
from django.db import models
class Recipe(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Ingredient(models.Model):
name = models.CharField(max_length=100)
quantity = models.FloatField()
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name="ingredients")
def __str__(self):
return self.name
The forms bound together:
# forms.py
from django import forms
from .models import Recipe, Ingredient
class IngredientForm(forms.ModelForm):
class Meta:
model = Ingredient
exclude = ('recipe',)
IngredientFormSet = forms.inlineformset_factory(Recipe, Ingredient, form=IngredientForm)
class RecipeForm(forms.ModelForm):
class Meta:
model = Recipe
fields = '__all__'
template_name = 'recipe_form.html'
def __init__(self, *args, **kwargs):
"""Initialize the formset with its fields."""
self.formset = IngredientFormSet(*args, **kwargs)
super().__init__(*args, **kwargs)
def get_context(self):
"""Add the formset to the context for rendering."""
context = super().get_context()
context['formset'] = self.formset
return context
def save(self, commit=True):
"""Bind both models together."""
instance = super().save(commit=False)
self.formset.instance = instance
if self.formset.is_valid():
instance.save()
self.formset.save()
return instance
The template for the form:
<!-- templates/recipe_form.html -->
<p>Recipe: {{ form.name }}</p> <!-- calling "form" creates a rendering recursion -->
<p>Ingredients:</p>
{{ formset.management_form }}
<ul>
{% for elt in formset %}
<li>{{ elt }}</li>
{% endfor %}
</ul>
And the view using it:
# views.py
from django.views.generic import TemplateView
from .forms import RecipeForm
class RecipeView(TemplateView):
template_name = 'recipe.html'
def get_context_data(self):
context = super().get_context_data()
context['form'] = RecipeForm()
return context
def post(self, *args, **kwargs):
form = RecipeForm(self.request.POST)
if form.is_valid():
form.save()
else:
raise Exception('Something bad happened!')
return self.get(*args, **kwargs)
With a very basic template:
<!-- templates/recipe.html -->
<form action="." method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Submit</button>
</form>
And finally you're good to go:
# tests.py
from django.test import TestCase
from django.urls import reverse
from .models import Recipe
class TestFormSet(TestCase):
def test_new_recipe(self):
data = {
"name": "quiche",
"ingredients-TOTAL_FORMS": 3,
"ingredients-INITIAL_FORMS": 0,
"ingredients-0-name": 'bacon bits',
"ingredients-0-quantity": 200,
"ingredients-1-name": 'eggs',
"ingredients-1-quantity": 4,
"ingredients-2-name": 'cream',
"ingredients-2-quantity": 150,
}
r = self.client.post(reverse('recipe'), data=data)
self.assertEqual(Recipe.objects.first().ingredients.count(),3)
$ python manage.py test
OK
Hopefully it will be useful to somebody.