Django not validating Class-Based Views correctly - django

In a Django 1.9 online learning app, I have a form that I am making using the generic class-based view CreateView as wells as some other mixins. In the form the 'owner' is able to create a new course, but upon form submission I get the following error:
IntegrityError at /course/create/
NOT NULL constraint failed: courses_course.owner_id
I realize that I am getting the error, because the form isn't saving the owner, which is part of the Course model. And I know the owner isn't saving because the app is not even entering my form_valid method in OwnerEditMixin class. (My console is not printing the form validating ... message in the method.)
Here is the code from my view:
from django.core.urlresolvers import reverse_lazy
from django.views.generic.edit import CreateView
from .models import Course
from django.contrib.auth.mixins import LoginRequiredMixin, \
PermissionRequiredMixin
class OwnerMixin(object):
def get_queryset(self):
qs = super(OwnerMixin, self).get_queryset()
return qs.filter(owner=self.request.user)
class OwnerEditMixin(object):
def form_valid(self, form):
print('form validating ...')
form.instance.owner = self.request.user
return super(OwnerEditMixin, self).form_valid(form)
class OwnerCourseMixin(OwnerMixin, LoginRequiredMixin):
model = Course
class OwnerCourseEditMixin(OwnerCourseMixin, OwnerEditMixin):
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
template_name = 'courses/manage/course/form.html'
class CourseCreateView(OwnerCourseEditMixin, CreateView,
PermissionRequiredMixin):
permission_required = 'courses.add_course'
The relevant template code is here:
<form action="." method="post">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Save course"></p>
</form>
Any ideas for why the class-based view isn't calling my form_valid method?
PS: I followed the example for class-based views form validation from https://docs.djangoproject.com/en/1.9/topics/class-based-views/generic-editing/#models-and-request-user

Related

I cant view my form inputs in the django database

I am a learner in django. i have created my first form, but when i input data i dont see it in my database but see in in my shell. I have rechecked my code but it seem fine, yet still it wont save in my code in the database.
Please help me out here.Thanks.
Model:
from django.db import models
class Login(models.Model):
first_name = models.CharField(max_length=200)
second_name = models.CharField(max_length=200)
email = models.EmailField()
password = models.CharField(max_length=200)
View:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Login
from .forms import login_form
def homeview(request):
return HttpResponse("<h1>Hello There</h1>")
def login_view(request):
form = login_form(request.POST or None)
if form.is_valid:
form.save
context = {
"form":form
}
return render(request, "login.html", context)
Template:
{% block content %}
<form action="" method="GET">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Login"/>
</form>
{% endblock %}
Form:
from django import forms
from .models import Login
class login_form(forms.ModelForm):
class Meta:
model = Login
fields = [
"first_name",
"second_name",
"email",
"password"
]
Following lines are incorrect:
if form.is_valid:
form.save
Currently the if will always return True because .is_valid returns the bound method.
You need to call is_valid -> form.is_valid()
Same for form.save. You would only return the bound method save, but you don't call it.
These lines would look like this:
if form.is_valid():
form.save()
Furthermore: In your template the used method for your form is GET but you are accessing request.POST. You need to change either one of them to the other method, e.g. method="POST".

Django class based views form submit 405 method not allowed

I'm trying to make a email submit form in a django app. Coming from Flask though, I'm a bit confused as I'm trying to do this with class based views, but am pretty stuck.
I'm currently getting this error but unsure how to make it successfully post
Method Not Allowed (POST): /newsletter/
Method Not Allowed: /newsletter/
My models class has this
class Newsletter(models.Model):
email = models.CharField(max_length=200, unique=True)
My forms.py has this
from django import forms
class NewsletterForm(forms.Form):
email = forms.CharField(max_length=200)
def send_email(self):
# send email using the self.cleaned_data dictionary
pass
my urls file has this
path('newsletter/', views.NewsletterView.as_view(), name='newsletter'),
and my form submit in my html is like this
<form action="/newsletter/" method="post">{% csrf_token %}
<label for="email">Email: </label>
<input id="email" type="email" name="email_field" placeholder="email#example.com">
<input type="submit" value="Subscribe">
</form>
And here is the views function
from django.views import generic
from .models import Post
from blog.forms import NewsletterForm
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
paginate_by = 3
class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
class NewsletterView(generic.TemplateView):
template_name = "newsletter.html"
form_class = NewsletterForm
success_url = '/thanks/'
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
form.send_email()
return super().form_valid(form)
You have to define a post method in your NewsletterView:
def post(self, request, *args, **kwargs):
...
Check this out ;) https://docs.djangoproject.com/en/3.0/topics/class-based-views/intro/#handling-forms-with-class-based-views

How can I use "add_fields" in django?

I was reading django docs and found the add_fields method.
The documentation says:
"If you need to add additional fields to the formset this can be
easily accomplished. The formset base class provides an add_fields
method."
I want to use it but there is no example or explanation how
it can be used in views and templates. Can you provide me a small
example of using this method?
I will give a small example.
The models.py looks like this,
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
pub_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
forms.py looks like this,
from django import forms
from django.forms import BaseFormSet
from .models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ['title']
class BaseArticleFormSet(BaseFormSet):
def add_fields(self, form, index):
super().add_fields(form, index)
form.fields['body'] = forms.CharField()
views.py looks like this,
from django.shortcuts import render
from django.forms import formset_factory
from .forms import ArticleForm, BaseArticleFormSet
def home(request):
ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet, extra=3)
context = dict()
formset = ArticleFormSet()
if request.method == 'POST':
formset = ArticleFormSet(request.POST)
if formset.is_valid():
print(formset.cleaned_data)
context['formset'] = formset
return render(request, 'home.html', context)
Finally, home.html will look like this,
<form method="post">
{% csrf_token %}
<table>
{{ formset }}
</table>
<button type="submit">submit</button>
</form>
As you can see, the body field appears in templates but it is not included in the the Article model.
Hope that helps!

Posting data from a form into the database using Django

I am trying to have a user input a task from the frontend and have that data instantiate a new model and add this new field in the database associated with their account. I have tried the following;
Profile HTML
<form id="taskitem_form" method="post" action="/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
<input type="submit" name="submit" value="Add Task" class ="btn btn-primary" />
</form>
Model
class TaskItem(models.Model):
taskn = models.CharField(max_length = 400)
usern = models.ForeignKey(User)
def __str__(self):
return self.taskn
Views
def add_task(request):
# Get the context from the request.
#context = RequestContext(request)
# A HTTP POST?
if request.method == 'POST':
form = TaskItemForm(request.POST)
# Have we been provided with a valid form?
if form.is_valid():
task = form.save(commit=False)
task.usern = request.user
task.save()
# we should redirect after data modifying
return redirect('/user/%s' %(request.user))
else:
# If the request was not a POST, display the form to enter details.
return render(request, 'profile.html', {'form': form})
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render(request, 'profile.html', {'form': form})
Forms
from django import forms
from bkmks.models import TaskItem
class TaskItemForm(forms.ModelForm):
taskn = forms.CharField(max_length = 300, help_text = "Please enter your task")
# An inline class to provide additional information on the form.
class Meta:
fields = ('taskn', 'usern' )
#This is the association between the model and the model form
model = TaskItem
Lot's of Changes needed to your code.
I'm posting a working version so that you can try.
Put profile.html file as bkmks/templates/bkmks/profile.html
Get it working. Customize later.
profile.html
<form id="taskitem_form" method="post" action="">
{% csrf_token %}
{{form}}
<input type="submit" name="submit" value="Add Task" class ="btn btn-primary" />
</form>
model as it is.
views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, RequestContext, redirect
from .forms import TaskItemForm
#login_required
def add_task(request):
# Get the context from the request.
context = RequestContext(request)
# A HTTP POST?
if request.method == 'POST':
form = TaskItemForm(request.POST)
# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
task = form.save(commit=False)
task.usern = request.user
task.save()
# Redirect to home (/)
return redirect('/')
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors
else:
# If the request was not a POST, display the form to enter details.
form = TaskItemForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response('bkmks/profile.html', {'form': form}, context)
forms.py
class TaskItemForm(forms.ModelForm):
# task is changed to taskn
taskn = forms.CharField(max_length = 300, help_text = "Please enter your task")
# An inline class to provide additional information on the form.
class Meta:
fields = ('taskn',)
#This is the association between the model and the model form
model = TaskItem
If you get any error or data is not getting saved post here.
Going through Django tutorial will be an wise decision.
The below should do what you need. You really want to inherit 100% of everything from your model when you can. This insures all model validation trickles down to the form. I utilized verbose_name and help_text on the model to achieve this.
Models
from django.conf import settings
class TaskItem(models.Model):
taskn = models.CharField(
max_length=400,
verbose_name="task",
help_text="Please enter your task.",
)
usern = models.ForeignKey(
to=settings.AUTH_USER_MODEL,
related_name="tasks",
)
def __str__(self):
return self.taskn
For the forms, I have added a forms.HiddenInput widget to the user, assuming you want the user submitting the task to become the user.
Forms
from django import forms
from bkmks.models import TaskItem
class TaskItemForm(forms.ModelForm):
widgets = {
'user': forms.HiddenInput,
}
class Meta:
model = TaskItem
fields = ('taskn', 'usern')
I have used a CreateView to reduce code complexity, and overrode the form_valid to add the user instance to the form.
Views
from django.views.generic import CreateView
from bkmks.models import TaskItem
from bkmks.forms import TaskItemForm
class TaskCreateView(CreateView):
model = TaskItem
form_class = TaskItemForm
template_name = "path/to/template.html"
def form_valid(self, form):
form.instance.user = self.request.user
return super(TaskCreateView, self).form_valid(form)
Finally, in the template, we simply want to use {{ form }}. I see you are looking into bootstrap. I'll suggest django-crispy-forms for this, but that is beyond the scope of your question.
Template
<form id="taskitem_form" method="post" action="/">
{% csrf_token %}
{{ form }}
<input type="submit" name="submit" value="Add Task" class ="btn btn-primary" />
</form>
https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render-to-response
render_to_response expects a template as the first argument, not a url.
I think in your second call to render_to_response should include the template name / path , while the first one should use a return HttpResponseRedirect("/") instead, though its not clear exactly what your problem is.
Add this line to imports in views.py
from django.contrib.auth.decorators import login_required
Decorate add_task view
#login_required
def add_task(request):
Then, edit part of your code
if form.is_valid():
task = form.save(commit=False)
task.usern = request.user
task.save()
# we should redirect after data modifying
return redirect('/')
else:
# etc.
Some notes. You may replace render_to_response to render.
Remove this line
context = RequestContext(request)
Replace
# Wrong usage, actually.
# Should be something like
# render_to_response(template_name, context, context_instance)
render_to_respone('/', {'form': form}, context)
with
# if template_name is "profile.html"
render(request, 'profile.html', {'form': form})
Why define a field called task in the form if you've already got a field in the model called taskn, wouldn't it be better to just use that? And like the guys have said, you need to specify a template to render (that's why you're not seeing anything).
It'd also be a good idea to pass the current user to the form's user field.
#login_required
def add_task(request):
# Get the context from the request.
context = {}
# A HTTP POST?
if request.method == 'POST':
form = TaskItemForm(request.POST)
# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
form.save()
# Now call the index() view.
# The user will be shown the homepage.
return render_to_response(
'profile.html',
{'form': form},
RequestContext(request, context)
)
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors
else:
# If the request was not a POST, display the form to enter details.
form = TaskItemForm(initial={'usern': request.user})
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response(
'profile.html',
{'form': form},
RequestContext(
request, context
)
)
Form;
from django import forms
from bkmks.models import TaskItem
class TaskItemForm(forms.ModelForm):
taskn = forms.CharField(max_length = 300, help_text = "Please enter your task")
# An inline class to provide additional information on the form.
class Meta:
fields = ('taskn', 'usern' )
#This is the association between the model and the model form
model = TaskItem

Django - Class Based Generic View - "No URL to redirect to"

I'm using the generic CreateView like:
#urls.py
from django.conf.urls.defaults import *
from django.views.generic import CreateView
from content.models import myModel
urlpatterns = patterns('myApp.views',
(r'myCreate/$', CreateView.as_view(model=myModel)),
)
With a mymodel_form.html template like:
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
When I submit my form, the new object is created but I get the error
ImproperlyConfigured at ...
No URL to
redirect to. Either provide a url or
define a get_absolute_url method on
the Model.
How can I specify the url to redirect on success ?
Have you tried passing in success_url? e.g.
CreateView.as_view(model=myModel, success_url="/success/")
or if you want to redirect to a named view:
CreateView.as_view(model=myModel, success_url=reverse('success-url'))
you can also try to define get_absolute_url in your models. For example
class Something(models.Model):
name = models.CharField(max_length=50, verbose_name='name')
class Meta:
pass
def get_absolute_url(self):
return u'/some_url/%d' % self.id
Add in views:
def form_valid(self, form):
return self.render_to_response(self.get_context_data(form=form))
views.py/
from django.views.generic import CreateView
from .models import myModel
class createView(CreateView):
model = myModel
fields = ['your fields', ...'']
def get_success_url(self):
return reverse('some_url')
urls.py/ (of your app)
from django.urls import path
from . import views
urlpatterns = [
path('create/', views.createView.as_view(), name='create')
]
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
success_url = '/'
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False