Hello I have problem with saving forms to database. When I try to save the AdHistoryForm in ads_history_add view the forim is rendered correctly but after submitting nothing happens aside of redirecting me to ads_history_list view.
In addition when I try to submit this form with empty field it doesnt show any errors (I included them in template), so maybe it is validation thing.
When I try to add Ad in ads_add view everything is ok.
Can you help me?
models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
class Ad(models.Model):
title = models.CharField(max_length=128, verbose_name=_("name"), help_text=_("required"), unique=True)
content = models.TextField(verbose_name=_("content"), blank=True)
url = models.URLField(verbose_name=_("website"), blank=True)
date_create = models.DateTimeField(auto_now_add=True)
date_modify = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
class AdHistory(models.Model):
ad = models.ForeignKey(Ad)
user = models.ForeignKey(User)
comment = models.TextField(verbose_name=_("comment"), help_text=_("required"))
date_create = models.DateTimeField(auto_now_add=True)
date_modify = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.comment
forms.py
from django import forms
from .models import Ad, AdHistory
class AdForm(forms.ModelForm):
class Meta:
model = Ad
fields = ['title', 'content', 'url']
class AdHistoryForm(forms.ModelForm):
class Meta:
model = AdHistory
fields = ['comment']
views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required, user_passes_test
from django.utils.translation import ugettext as _
from .models import Ad, AdHistory
from .forms import AdForm, AdHistoryForm
#login_required
#user_passes_test(lambda u: u.is_superuser)
def ads_list(request):
ads_list = Ad.objects.all().order_by('-date_modify')
context = {'list': ads_list}
return render(request, 'ads_list.html', context)
#login_required
#user_passes_test(lambda u: u.is_superuser)
def ads_add(request):
form = AdForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('ads_list')
context = {'form': form}
return render(request, 'ads_form_add.html', context)
#login_required
#user_passes_test(lambda u: u.is_superuser)
def ads_history_list(request, ad_id):
ad = get_object_or_404(Ad, pk=ad_id)
history_list = AdHistory.objects.select_related().filter(ad=ad).order_by('-id')
context = {'list': history_list, 'object': ad}
return render(request, 'ads_history_list.html', context)
#login_required
#user_passes_test(lambda u: u.is_superuser)
def ads_history_add(request, ad_id):
ad = get_object_or_404(Ad, pk=ad_id)
f = AdHistoryForm(request.POST or None)
if f.is_valid():
new_entry = f.save(commit=False)
new_entry.ad = ad
new_entry.user = request.user
new_entry.save()
return redirect('ads_history_list', ad_id)
context = {'form': f, 'object': ad}
return render(request, 'ads_history_add.html', context)
urls.py
rom django.conf.urls import patterns, url
from django.contrib.auth.decorators import login_required
from ads import views
urlpatterns = patterns(
'',
url(r'^$', views.ads_list, name="ads_list"),
url(r'^add/', views.ads_add, name="ads_add"),
url(r'^(?P<ad_id>\d+)/history/$', views.ads_history_list, name="ads_history_list"),
url(r'^(?P<ad_id>\d+)/history/add$', views.ads_history_add, name="ads_history_add"),
)
both form templates inherits from this template:
<form role="form" method="post" action=".">
{% csrf_token %}
<table class="table table-bordered crm-form">
{% for field in form.visible_fields %}
<tr>
<th>
{{ field.label }}
</th>
<td>
{{ field }}
<small>{{ field.help_text }}</small>
{% if field.errors %}
<div class="alert alert-danger" role="alert">{{ field.errors }}</div>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
<button type="submit" name="submit" class="btn btn-success crm-float-right">
{% trans 'Save' %}
</button>
</form>
The POST request never reaches your ads_history_add view because your ads_history_add URL pattern does not have a trailing slash. Without the trailing slash, action="." in the ads_form_add.html template results in a POST to (?P<ad_id>\d+)/history/
Add the trailing slash and everything should work as expected. Alternatively, you could omit the action attribute to tell the browser to POST to the current URL.
Also note that, although not relevant here, it is probably a good habit to display {{ form.non_field_errors }}.
Related
Good afternoon all,
One of my form does not seem to save when submitted. I cannot see why, in particular as I have a similar form working just fine using the same code.
For some reason it work just fine using the admin panel.
My assumption is that I am missing something that tells the form it needs to be saved. But cannot find what.
Any ideas?
Models
RATING=(
(1,'1'),
(2,'2'),
(3,'3'),
(4,'4'),
(5,'5'),
)
class ProductReview(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE)
product=models.ForeignKey(Product,related_name="comments", on_delete=models.CASCADE)
review_text=models.TextField(max_length=250)
review_rating=models.IntegerField(choices=RATING,max_length=150, default=0)
date_added = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
Views
def add_review(request, product_id):
product = Product.objects.get(pk=product_id)
form = ReviewAdd(request.POST or None, instance=product) #instance=product (populate field with existing information)
if form.is_valid():
form.save()
return redirect('product')
return render(request, 'main/add_review.html',{'form':form})
URL
from django.urls import path
from . import views
urlpatterns = [
...
path('product/add_review/<product_id>', views.add_review,name="add_review"),
]
Forms
class ReviewAdd(forms.ModelForm):
class Meta:
model = ProductReview
fields = ('review_text', 'review_rating')
labels ={
'review_text': '',
'review_rating': '',
}
widgets = {
'review_text': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter Review'}),
}
Admin
from django.contrib import admin
from .models import Venue, User, Product, ProductReview
from django.urls import path
admin.site.register(User)
admin.site.register(ProductReview)
class ProductReview(admin.ModelAdmin):
list_display=['user','product','review_text','get_review_rating']
HTML Page
{% extends 'main/base.html' %}
{% load crispy_forms_tags %}
{% block title %}
{% endblock %}
{% block content %}
<center>
<h1>Add ReviewTo Database</h1>
<br/><br/>
{% if submitted %}
Success!
{% else %}
<form action="" method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="Submit" value="Submit" class="btn btn-secondary">
</form>
{% endif %}
</center>
{% endblock %}
I detect 2 fixes
On your url, the parameter product_id might need the type of data it will going to receive
path('product/add_review/<int:product_id>', views.add_review,name="add_review"),
And in your view, you are sending an instance, and no data for a new rating.
In your view:
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
#login_required
def add_review(request, product_id):
product = get_object_or_404(Product, pk=product_id)
form = ReviewAdd(request.POST or None)
if form.is_valid():
new_rating = form.save(commit=False)
new_rating.product = product
new_rating.user = request.user
new_rating.save()
return redirect('product')
return render(request, 'main/add_review.html',{'form':form})
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 }}
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)
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.
I've seen similar types of problems on here but I still haven't been able to work out my problem. When I save PhotographerProfileForm in my view, the form is rendered correctly but after clicking update/submit nothing is actually saved.
No errors are shown either.
What I have now are the fields are prepopulated and I would like the ability to save over these in the database, but nothing happens and at the moment you can only update it from the admin panel.
models.py
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class PhotographerProfile(models.Model):
user = models.OneToOneField(User)
location = models.CharField(max_length=200)
bio = models.TextField(max_length=500)
def __str__(self):
return self.user.username
User.profile = property(lambda u: PhotographerProfile.objects.get_or_create(user=u)[0])
from django.db.models.signals import post_save
from django.dispatch import receiver
#receiver(post_save, sender=User)
def create_profile(sender,instance,created,**kwargs):
if created:
profile, new = PhotographerProfile.objects.get_or_create(user=instance)
urls.py
urlpatterns = [
url(r'^profile/$', 'photoprofile.views.photographer_profile', name = 'photographer_profile'),
url(r'^profile/portfolio/$', 'photoprofile.views.photographer_portfolio', name='photographer_portfolio'),
]
views.py
#login_required
def photographer_profile(request):
photographerProfile = PhotographerProfile.objects.get(user=request.user)
form = PhotographerProfileForm(initial={'bio':photographerProfile.bio, 'location':photographerProfile.location})#This means you can prepoulate it
return render_to_response('photographerprofile.html',{'form':form}, RequestContext(request))
if request.method == 'POST':
form = PhotographerProfileForm(request.POST, instance = request.user.profile,)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/profile')
else:
user = request.user
profile = user.profile
form = PhotographerProfileForm()
return render(request, 'photographerprofile.html', {'form':form})
def photographer_portfolio(request):
photographerProfile = PhotographerProfile.objects.get(user=request.user)
return render_to_response('photographerportfolio.html', {'photographerProfile':photographerProfile}, RequestContext(request))
forms.py
class PhotographerProfileForm(forms.ModelForm):
class Meta:
model = PhotographerProfile
exclude = ('user',)
photographerprofile.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<h2> Profile</h2>
{% for field in form %}
{{ field.error}}
{% endfor %}
<form action='/accounts/profile/' method='post'>
{% csrf_token %}
{{form|crispy}}
<input type='submit' value='Update'/>
<p>Click <a href='/accounts/profile/portfolio/'>here</a> to view portfolio. </p>
</form>
{% endblock %}
You return in the third line of your view, before the form has been processed. Remove that line.