Django form not passing is_valid() test - python

In my Django app in a Createview class it never enters the is_valid(): statement and I can not seem to find any errors:
models.py
from django.db import models
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.conf import settings
from .validators import validate_file_extension
import zipfile
class Post(models.Model):
title = models.CharField(max_length=140)
body = models.TextField(max_length=250)
date = models.DateTimeField(auto_now=True, auto_now_add=False)
album_image = models.FileField(validators=[validate_file_extension])
user = models.ForeignKey(User, default=1)
face = models.IntegerField(default=1)
def get_absolute_url(self):
return reverse('photos:detail',kwargs={'pk':self.pk})
def __str__(self):
return self.title
views.py
This is my view folder that contains a list view a detailed view and create view. Although the form doesnt pass the valid test, it still gets uploaded and is viewable by the user
from django.http import Http404
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.core.urlresolvers import reverse
from .forms import PostForm
from .models import Post
from django.contrib.auth.models import User
from django.template import loader
from django.views import generic
from django.views.generic.edit import CreateView
import cognitive_face as CF
import json
class IndexView(generic.ListView):
template_name='photos/post.html'
def get_queryset(self):
return Post.objects.filter(user=self.request.user)
class DetailView(generic.DetailView):
model = Post
template_name = 'photos/detail.html'
class PostCreate(generic.CreateView):
form = PostForm()
model = Post
if form.is_valid():
print('valid')
instance = form.save(commit=False)
username = form.cleaned_data['username']
album_image = form.cleaned_data['album_image']
instance.save()
if not form.is_valid():
print('not')
post_form.html
<html>
<body>
{% if request.user.is_authenticated%}
<h3>Hello {{request.user.username}}, please upload your image as either a .JPEG, .GIF or .PNG</h3>
{% endif %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-7">
<div class="panel panel-default">
<div class="panel-body">
{% if request.user.is_authenticated %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
{{ form.errors }}
{{ form.non_field_errors }}
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
{% else %}
<p>You must be logged in to upload a file</p>
{% endif %}
</div>
</div>
</div>
</body>
</html>
urls.py
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from photos.models import Post
from . import views
app_name = 'photos'
urlpatterns = [
url(r'^$',views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='detail'),
url(r'post/add/$', views.PostCreate.as_view(), name='post-add'),
]

You are writing function based view code inside a class based view, which is incorrect.
You shouldn't need to instantiate the form, or manually check whether it is valid. Just set form_class for the view, then override form_valid or form_invalid if you need to change the behaviour when the form is valid or invalid. Since you have {{ form.errors }} in your template, it should show any errors when you submit the form.
class PostCreate(generic.CreateView):
form_class = PostForm
model = Post
See the docs on form handling with class based views for more information. You might find it easier to write a function based view to begin with, since the flow of the code is easier to follow.

Related

django model data returns none in html but shows in admin panel

I've created a model that lets users create a simple post however when the data is submitted it registers in the admin panel but will not display in the html. I can edit in admin and the images even up in the folder unfortunately I cannot find out what the issue is.
models.py
from distutils.command.upload import upload
from django.db import models
from django.forms import CharField, ImageField
from django.forms.fields import DateTimeField
from matplotlib import image
from sqlalchemy import true
from django.contrib import admin
class Posts(models.Model):
image = models.ImageField(upload_to='images/')
quote = models.TextField(null=True)
date = models.DateTimeField(auto_now_add=True,
null=True)
views.py
from django.contrib.auth import login, authenticate
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
from requests import request
from users.forms import SignUpForm
from matplotlib.style import context
from django.http import HttpRequest, HttpResponse
from .models import Posts
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('profile') #profile
else:
form = SignUpForm() #was UserCreationForm
return render(request, 'signup.html', {'form': form})
def posts(request):
userpost = Posts.objects.all()
return render(request, 'posts.html', {'userpost': Posts} )
urls.py
from django.urls import path
from django import urls
from django.conf.urls import url
from . import views
from django.contrib.auth.views import LoginView
urlpatterns = [
path('login/', LoginView, {'template_name': 'users/login.html'}, name = 'login'),
url('signup/',views.signup, name = 'signup'),
url('posts/',views.posts, name = 'posts'),
]
Template file:
<div class="container mt-5">
<div class="row">
<div class="col-sm-6">
<div class="card-header">
{{ userpost.quote }}
<br>
<div class="img">
<a href=""><img src="{{ userpost.image.url
}}"></a>
</div>
<div class="card-body">
</div>
</div>
</div>
<div class="col-sm-6">
<em>
{{ userpost.date }}
</em>
I receive no errors but am unsure as to what the issue is.
If you look at it correctly the mistake is in the posts view itself.
It must be {'userpost': userpost} not {'userpost': Posts} .
Try this:
views.py
def posts(request):
userpost = Posts.objects.all()
return render(request, 'posts.html', {'userpost': userpost})
Then, you should loop your userpost in following way, its only a minimal reproductible example, as I am not able to understand the design but it should display your data:
Template file:
<div class="container mt-5">
<div class="row">
<div class="col-sm-6">
{% for single_post in userpost %}
<div class="card-header">
{{ single_post.quote }}
<br>
<div class="img">
<a href=""><img src="{{single_post.image.url
}}"></a>
</div>
<div class="card-body">
</div>
</div>
</div>
<div class="col-sm-6">
<em>
{{ single_post.date }}
</em>
{% endfor %}
Note: Models in django are generally written in singular form, It will be better if you name it as only Post instead of Posts, as s is by default added in models as the suffix.

Save user who submitted form(django)

So I'm creating a reporting app for my organization I want to be able to save the user that submits the report. Problem is if I use the models.ForeignKey(User,on_delete=models.PROTECT) method on my model I get a drop down for all the users which is not what I want.
models.py
class Report(models.Model):
id = models.UUIDField(primary_key=True,default=uuid.uuid1,editable=False)
department = models.ForeignKey(Company,null=True,on_delete=models.SET_NULL)
user= models.ForeignKey(User,on_delete=models.PROTECT)
submission_date= models.DateField(auto_now=True) #invisible to user
submission_time = models.TimeField(auto_now=True) #invisible to ,user
date = models.DateField(default=now,blank=False)
time = models.TimeField(default=now,blank=False,help_text="hh:mm:ss")
location = PlainLocationField()
building = models.ForeignKey(bld,null=True,on_delete=models.SET_NULL)
size = models.PositiveIntegerField()
notes = models.TextField(blank=True)
def __str__(self):
return f'{self.date} {self.time} ({self.company})
form.py
from django.forms import ModelForm, fields
from django.shortcuts import redirect
from django.urls.conf import include
from .models import Report
from django import forms
from location_field.forms.plain import PlainLocationField
class ReportForm(forms.ModelForm):
class Meta:
model = Report
fields = '__all__'
location = PlainLocationField()
def redirect():
return redirect("Report")
views.py
from django.forms import fields
from django.forms.forms import Form
from django.http import request
from django.http.request import HttpRequest, validate_host
from django.http.response import HttpResponse, HttpResponseRedirect
from django.shortcuts import render,redirect
from django.urls.base import reverse
from django.views.generic.base import TemplateView
from django.contrib.auth import authenticate, login
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.
from .forms import ReportForm
from .models import Report
from django.views.generic.edit import CreateView, UpdateView, DeleteView
class ReportCreate(LoginRequiredMixin,CreateView):
Template = "templates\reports\report.html"
model = Report
fields = '__all__'
def form_valid(self, form):
return super().form_valid(form)
def get_success_url(self):
return reverse('Report')
def home(request):
return render(request,"users/home.html")
EDIT:
templates/report_form.html
{% extends "base_generic.html" %}
<head>
</head>
<body>
{% block content %}
<form action="" method="post" onsubmit="return True">
{% csrf_token %}
<table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<input type="hidden" name="user" value= "{{ request.user }}">
{{ form.as_table }}
{{ form.media }}
</table>
<input type="submit" value="Submit">
</form>
{% endblock %}
</body>
There's a few of ways to go about this.
You can add blank=True to user model field and assign the user when overriding the form_valid method:
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.save()
return super(ReportCreate, self).form_valid(form)
Or, render the user form field as a hidden field and pre-populate it:
<input type="hidden" name="user" value="{{ request.user }}

ValueError at /create_entry/

Can someone help me to solve this error?
ValueError at /create_entry/
Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x000000B7BBF1BFC8>>": "Entry.entry_author" must be a "User" instance.
urls.py
from django.urls import path
from .views import HomeView, EntryView, CreateEntryView
urlpatterns = [
path('', HomeView.as_view(), name = 'blog-home'),
path('entry/<int:pk>/', EntryView.as_view(), name = 'entry-detail'),
path('create_entry/', CreateEntryView.as_view(success_url='/'), name = 'create_entry')
]
views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import Entry
class HomeView(ListView):
model = Entry
template_name = 'entries/index.html'
context_object_name = "blog_entries"
class EntryView(DetailView):
model = Entry
template_name = 'entries/entry_detail.html'
class CreateEntryView(CreateView):
model = Entry
template_name = 'entries/create_entry.html'
fields = ['entry_title', 'entry_text']
def form_valid(self,form):
form.instance.entry_author = self.request.user
return super().form_valid(form)
models.py
from django.db import models
from django.contrib.auth.models import User
class Entry(models.Model):
entry_title=models.CharField(max_length=50)
entry_text=models.TextField()
entry_date=models.DateTimeField(auto_now_add=True)
entry_author=models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "entries"
def __str__(self):
return f'{self.entry_title}'
create_entry.html
{% extends "entries/base.html" %}
{% block content %}
<div class="col-md-8"><br><br>
<!-- Blog Post -->
<div class="card mb-4">
<div class="card-header">
Create Blog Post
</div>
<div class="card-body">
<form class="form-conrol" action="" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-primary">Post Entry</button>
</form>
</div>
</div>
</div>
{% endblock %}
I need your help for this
small project.
You are not logged in, so self.request.user is not a real user. You can use the LoginRequiredMixin [Django-doc] to restrict access to a view such that you can only post (and retrieve) the view when the user has logged in:
from django.contrib.auth.mixins import LoginRequiredMixin
class CreateEntryView(LoginRequiredMixin, CreateView):
model = Entry
template_name = 'entries/create_entry.html'
fields = ['entry_title', 'entry_text']
def form_valid(self,form):
form.instance.entry_author = self.request.user
return super().form_valid(form)

Getting forms to display in HTML with Django

so I've gotten forms to display correctly before, I'm just a little lost as to why I am using certain words when doing so, and I'm wondering why I've used different words to get different forms to display.
In this first example (colorist_form.html) I am using {{ form }} which does get the form to display
{% extends "base.html" %}
{% block content %}
<div class="colorset-base">
<h2>Create new post</h2>
<p class="hint">Add hex codes for each color you would like to include.</p>
<form id="postForm" action="{% url 'colorsets:new_color' %}" method="POST">
{% csrf_token %}
{{ form }}
<button type="submit" class="submit btn btn-primary btn-large">Add Color Set</button>
</form>
</div>
{% endblock %}
However, in this example (widget_form.html) I am using the same {{ form }} but now the form does not display
{% block content %}
<div class="colorset-base">
<h2>Create new widget</h2>
<form id="postForm" action="{% url 'adminpanel:create-widget' %}" method="POST">
{% csrf_token %}
{{ form }}
<button type="submit" class="submit btn btn-primary btn-large">Add Widget</button>
</form>
</div>
{% endblock %}
Also in my register.html I am using {{ user_form }} which does get the form to display.
{% extends "base.html" %}
{% block content %}
<div class="form-base">
{% if registered %}
<h1>Thank you for registering!</h1>
{% else %}
<h2>Register</h2>
<form method="POST">
{% csrf_token %}
{{ user_form }}
<input type="submit" value="Register" />
</form>
{% endif %}
</div>
{% endblock %}
Why do I use the keyword "form" vs some other word such as "user_form" or "widget_form"? And why won't the widget_form.html display the form?
Here is my code for the forms and views respectively:
adminpanel app views.py:
from django.shortcuts import render
from adminpanel.forms import WidgetForm
from adminpanel.models import Widget
from django.utils import timezone
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse,reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from braces.views import SelectRelatedMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
class CreateWidgetView(LoginRequiredMixin,CreateView):
login_url = '/login/'
redirect_field_name = 'index.html'
form_class = WidgetForm
model = Widget
def form_valid(self,form):
self.object = form.save(commit=False)
self.object.save()
return super().form_valid(form)
class SettingsListView(ListView):
model = Widget
def get_query(self):
return Widget.object.filter(order_by('widget_order'))
class DeleteWidget(LoginRequiredMixin,SelectRelatedMixin,DeleteView):
model = Widget
select_related = ('Widget',)
success_url = reverse_lazy('settings')
def get_queryset(self):
queryset = super().get_query()
return queryset.filter(user_id=self.request.user.id)
def delete(self):
return super().delete(*args,**kwargs)
colorsets app views.py:
from django.shortcuts import render
from colorsets.forms import ColorForm
from colorsets import models
from colorsets.models import ColorSet
from django.utils import timezone
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse,reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from braces.views import SelectRelatedMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
#def index(request):
# return render(request,'index.html')
class PostListView(ListView):
model = ColorSet
def get_queryset(self):
return ColorSet.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
class CreateColorSetView(LoginRequiredMixin,CreateView):
login_url = '/login/'
redirect_field_name = 'index.html'
form_class = ColorForm
model = ColorSet
def form_valid(self,form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return super().form_valid(form)
class DeletePost(LoginRequiredMixin,SelectRelatedMixin,DeleteView):
model = models.ColorSet
select_related = ('user',)
success_url = reverse_lazy('index')
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(user_id=self.request.user.id)
def delete(self,*args,**kwargs):
return super().delete(*args,**kwargs)
adminpanel app forms.py:
from django import forms
from adminpanel.models import Widget
class WidgetForm(forms.ModelForm):
class Meta():
model = Widget
fields = ('name','widget_order','body')
widgets = {
'name':forms.TextInput(attrs={'class':'text-input'}),
'body':forms.Textarea(attrs={'class':'text-area'}),
}
colorsets app forms.py:
from django import forms
from colorsets.models import ColorSet
class ColorForm(forms.ModelForm):
class Meta():
model = ColorSet
fields = ('name','color_one','color_two','color_three','color_four','color_five')
widgets = {
'name':forms.TextInput(attrs={'class':'colorset-name'}),
'color_one':forms.TextInput(attrs={'class':'colorset-color'}),
'color_two':forms.TextInput(attrs={'class':'colorset-color'}),
'color_three':forms.TextInput(attrs={'class':'colorset-color'}),
'color_four':forms.TextInput(attrs={'class':'colorset-color'}),
'color_five':forms.TextInput(attrs={'class':'colorset-color'}),
}
Not sure that all that code was needed for this problem but figured it couldn't hurt.
I'm a little lost (even though I've gotten this to work in the past) so any help would be most appreciated.
If you use create view you don't have to write the form, automatically Django generate the modelform so should be something like:
class CreateWidgetView(LoginRequiredMixin,CreateView):
login_url = '/login/'
redirect_field_name = 'index.html'
model = Widget
fields = ['field', 'field2']
def form_valid(self,form): #I Think this part is not neccesary
self.object = form.save(commit=False)
self.object.save()
return super().form_valid(form)

Django Contact Form using ModelForm

I am trying to create a contact form that will both email and store the message. I think I got the model.py, forms.py and admin.py right, and I am able to create and store (not email) a message from the admin. But I am struggling with the view.py that has to both email and store the message.
model.py:
from django.db import models
from django.contrib import admin
class Contact(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
message = models.TextField(max_length=10)
date_created = models.DateField(verbose_name="Created on date", auto_now_add="True")
class ContactAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'message', 'date_created')
forms.py:
from django import forms
from .models import Contact
class ContactForm(forms.Form):
name = forms.CharField()
email = forms.EmailField()
When it comes to the views.py, I need some guidance in order to put together the code for the def contact(request):. I think I will have to include these modules:
from django.conf import settings
from django.shortcuts import render, HttpResponseRedirect, HttpResponse, Http404
from django.core.mail import send_mail
from .forms import ContactForm
from .models import Contact
When it comes to the template part, I am not sure on how to use the template tags to render the contact form in html.
So, I need help to figure out the correct view and template code.. I am of course open to suggestions for the rest of the code as well - As you might guessed, this is my first real Django app.
Thank you!
Something like this:
I would use a django ModelForm to generate the form:
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
exclude = ('date_created', )
Docs: https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#modelform
And FormView for the actual view:
from django.conf import settings
from django.core.mail import send_mail
from django.views.generic import FormView
from .forms import ContactForm
class ContactFormView(FormView):
form_class = ContactForm
template_name = "email_form.html"
success_url = '/email-sent/'
def form_valid(self, form):
message = "{name} / {email} said: ".format(
name=form.cleaned_data.get('name'),
email=form.cleaned_data.get('email'))
message += "\n\n{0}".format(form.cleaned_data.get('message'))
send_mail(
subject=form.cleaned_data.get('subject').strip(),
message=message,
from_email='contact-form#myapp.com',
recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
)
form.save()
return super(ContactFormView, self).form_valid(form)
Docs: https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-editing/#formview
And your template:
{% extends 'base.html' %}
{% block title %}Send an email{% endblock %}
{% block content %}
<div class="row">
<div class="span6">
<h1>Send an email</h1>
<form action="." method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
</div>
</div>
{% endblock %}
{% block extrajs %}
<script src="{{ STATIC_URL }}js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
$('#id_name').focus()
});
</script>
{% endblock %}
Docs: https://docs.djangoproject.com/en/1.7/topics/forms/#the-template

Categories