Python - Update (edit) form not showing - python

I have been reading the django documentation, googling for days where none have the same problem as me. It seems that method "member_edit" in "Views.py" does not return any data and therefor can not show me the form.
Hopefully, some of you can spot where I went wrong. Appreciate all the help I can get!
Models.py
class Member(models.Model):
member_no = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50, null=True, blank=True)
last_name = models.CharField(max_length=50, null=True, blank=True)
email = models.CharField(max_length=50, null=True, blank=True)
reg_date = models.DateTimeField(null=True, blank=True)
class Meta:
db_table = 'Member'
Forms.py
class RegForm(forms.ModelForm):
first_name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
max_length=30,
required=True)
last_name = forms.CharField(
widget=forms.TextInput(attrs={'class': 'form-control'}),
max_length=30,
required=True)
email = forms.CharField(
widget=forms.EmailInput(attrs={'class': 'form-control'}),
required=True,
max_length=75)
reg_date = forms.DateField(widget=DateWidget(usel10n=True,bootstrap_version=3))
class Meta:
model = Member
exclude = ['last_login', 'date_joined']
fields = ['first_name', 'last_name', 'email', 'reg_date', ]
Views.py
def member_edit(request, member_no):
member = Member.objects.get(member_no=member_no)
if request.method == 'POST':
form = RegForm(request.POST or None, instance=member)
if form.is_valid():
member.first_name = form.request.POST['first_name']
member.last_name = form.request.POST['last_name']
member.email = form.request.POST['email']
member.reg_date = form.request.POST['reg_date']
member.save()
return redirect('member_overview')
return render(request, 'member/member_signup.html')
urls.py
urlpatterns = [
url(r'^member_edit/(?P<member_no>\d+)$', views.member_edit, name='member_edit')
]
member_edit.html
{% block body %}
<h1 class="logo">Members</h1>
<div class="signup">
<h2>{% trans 'Update member' %}</h2>
<form action="{% url 'member_edit' member.member_no %}" method="post" role="form">
{% csrf_token %}
{% for field in form.visible_fields %}
<div class="form-group{% if field.errors %} has-error{% endif %}">
<label for="{{ field.label }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field }}
{% if field.help_text %}
<span class="help-block">{{ field.help_text }}</span>
{% endif %}
{% for error in field.errors %}
<label class="control-label">{{ error }}</label>
{% endfor %}
</div>
</div>
{% endfor %}
<center>
<button type="submit" class="btn btn-primary">{% trans 'Update member' %}</button>
</center>
</form>
</div>
{% endblock body %}

You're not passing the form in the context (and the template name appears to be incorrect?):
def member_edit(request, member_no):
member = Member.objects.get(member_no=member_no)
if request.method == 'POST':
form = RegForm(request.POST or None, instance=member)
if form.is_valid():
# This isn't necessary - just save the form
#member.first_name = form.request.POST['first_name']
#member.last_name = form.request.POST['last_name']
#member.email = form.request.POST['email']
#member.reg_date = form.request.POST['reg_date']
#member.save()
form.save()
return redirect('member_overview')
# Instantiate the form
form = RegForm(instance=member)
# Add a dictionary for the context
return render(request, 'member/member_edit.html', {'form': form, 'member': member)
You could make this cleaner (and easier) though with a generic class based view:
from django.views import generic
from django.urls import reverse_lazy
class UpdateMember(generic.UpdateView):
model = Member
form_class = RegForm
pk_url_kwarg = 'member_no'
template_name = 'member/member_edit.html'
success_url = reverse_lazy('member_overview')
Think I got all the attributes you need there - you can check out CCBV it's a great resource for the class based views.
Take a look at the documentation on views and templates

Related

How to solve user updating problem in Django

I created a system with Django. I need user update section for this project. At the beginning of the project the user change form was working but now it doesnt work. I have a hiden field in my form and I think that's the problem but I'm not sure. Can you help me?
view.py
def update_user(request, id):
user = get_object_or_404(UserProfile, id=id)
form = SignUpForm(request.POST or None, request.FILES or None, instance=user)
if form.is_valid():
form.save()
return redirect('/')
context = {
'form': form,
}
return render(request, "update_user.html", context)
models.py
class UserProfile(AbstractUser):
ranks = (
('Analyst', 'Analyst'),
...
('Chief Financial Officer', 'Chief Financial Officer'),
)
comp_name = models.CharField(max_length=200, default='', blank=True, null=True)
user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
....
rank = models.CharField(max_length=200, choices=ranks)
image = models.ImageField(upload_to='profile_image', blank=True, null= True, default='profile.png')
def __str__(self):
return self.username
def get_unique_slug(self):
slug = slugify(self.slug.replace('ı', 'i'))
unique_slug = slug
counter = 1
while UserProfile.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, counter)
counter += 1
return unique_slug
forms.py
class SignUpChangeForm(UserChangeForm):
class Meta:
model = UserProfile
fields = (
'username', 'first_name', 'last_name', 'email', 'rank', 'comp_name', 'image'
)
def clean_password(self):
return self.clean_password
update_user.html
<form method="post">
{{ form|crispy }}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
{% csrf_token %}
<button type="submit" class="btn btn-success">Update</button>
</form>
Error in the form

cleaned_data() returns empty objects, when I try to upload multiple images in Django

models.py:
class Object(PolymorphicModel):
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
title = models.CharField(max_length=300)
city = models.ForeignKey(City, on_delete=models.CASCADE)
address = models.CharField(max_length=300)
phone = models.CharField(max_length=20, default='')
email = models.CharField(max_length=100, default='')
site = models.CharField(max_length=100, default='')
facebook = models.CharField(max_length=100, default='')
instagram = models.CharField(max_length=100, default='')
content = models.TextField()
rating = models.DecimalField(default=10.0, max_digits=5, decimal_places=2)
created_date = models.DateTimeField(default=timezone.now)
approved_object = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
def __str__(self):
return f"{self.title}"
class Restaurant(Object):
seats = models.IntegerField()
bulgarian_kitchen = models.BooleanField(default=False)
italian_kitchen = models.BooleanField(default=False)
french_kitchen = models.BooleanField(default=False)
category_en_name = models.CharField(max_length=100, default='restaurants')
category_bg_name = models.CharField(max_length=100, default='Ресторанти')
bg_name = models.CharField(max_length=100, default='Ресторант')
is_garden = models.BooleanField(default=False)
is_playground = models.BooleanField(default=False)
class Images(models.Model):
object = models.ForeignKey(Object, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='attachments',
verbose_name='Image')
forms.py:
class RestaurantForm(forms.ModelForm):
class Meta:
model = Restaurant
fields = [
'title',
'content',
'city',
'address',
'phone',
'email',
'site',
'facebook',
'instagram',
'seats',
'bulgarian_kitchen',
'italian_kitchen',
'french_kitchen',
'is_garden',
'is_playground'
]
class ImageForm(forms.ModelForm):
image = forms.ImageField(label='Снимка')
class Meta:
model = Images
fields = [
'image'
]
template (html):
<form method="post" id="dialog_addObject_part">
{% csrf_token %}
{% for hidden in postForm.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form %}
<div class="fieldWrapper">
<div class="errorcode{{field.html_name}}">
{{ field.errors }}
</div>
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<div class="utf_addObject_form">
<button type="submit" value="Изпрати">Изпрати</button>
</div>
</form>
views.py:
def add_object(request, category):
if not request.user.is_authenticated:
messages.info(request, 'За да добавите нов Обект, трябва да сте регистриран потребител!')
return redirect('account_login')
form = RestaurantForm(request.POST or None);
ImageFormSet = modelformset_factory(Images,
form=ImageForm, extra=3)
if request.method == 'POST':
formset = ImageFormSet(request.POST, request.FILES)
if form.is_valid() and formset.is_valid():
obj = form.save(commit=False)
obj.author = ProfileUser.objects.get(user=request.user)
obj.save()
print(formset.cleaned_data)
for form in formset.cleaned_data:
if form:
image = form['image']
photo = Images(post=form, image=image)
photo.save()
messages.success(request, 'Успешно добавихте нов Обект, може да видите вашите обекти във вашия профил!')
return redirect('home')
else:
formset = ImageFormSet(queryset=Images.objects.none())
context = {
'form': form,
'formset': formset
}
return render(request, "add_object.html", context)
This row print(formset.cleaned_data) returns me empty objects, so it doesn't upload images.
You missed enctype="multipart/form-data" in html form.
from docs:
Note that request.FILES will only contain data if the request method
was POST and the that posted the request has the attribute
enctype="multipart/form-data". Otherwise, request.FILES will be empty.
Form should be,
<form method="post" id="dialog_addObject_part" enctype="multipart/form-data">
</form>

Django form field is displaying uuid of the record

I am using django 2.2 and python 3.6.
I have a django modelform. I am using crispy forms to display the form in template.
The form is not displaying the value of the records. It is displaying the uuid values of the records. I need to display the record name value instead of uuid value.
models.py :
#reversion.register()
class Staff(BaseModel):
user = models.OneToOneField(
User, on_delete=models.PROTECT, db_index=True, verbose_name=_("Kullanıcı"))
photo = models.ImageField(
upload_to="staff/", null=True, blank=True, verbose_name=_("Fotoğraf"))
staff_type = models.ManyToManyField(
StaffType, verbose_name=_("Personel Tipi"))
name = models.CharField(
max_length=100, db_index=True, verbose_name=_("İsim"))
surname = models.CharField(
max_length=100, db_index=True, verbose_name=_("Soyad"))
phone = models.CharField(max_length=100, verbose_name=_("Telefon Numarası"))
email = models.EmailField(verbose_name=_("Email"), db_index=True)
address = models.TextField(verbose_name=_("Adres"))
subject = models.ForeignKey(Subject, on_delete=models.SET(
get_unknown_subject), verbose_name=_("Branş"))
gender = models.IntegerField(
choices=GENDERS, default=None, verbose_name=_("Cinsiyet"))
nationality = models.CharField(
choices=NATIONALITIES, max_length=100, verbose_name=_("Uyruk"))
blood_type = models.CharField(
choices=BLOOD_TYPES, max_length=20, verbose_name=_("Kan Grubu"))
id_no = models.CharField(max_length=100, unique=True,
verbose_name=_("Kimlik No"))
birthdate = models.DateField(verbose_name=_("Doğum Günü"))
birthplace = models.CharField(max_length=200, verbose_name=_("Doğum Yeri"))
education = models.IntegerField(
choices=EDUCATION, default=None, verbose_name=_("Eğitim"))
marital_status = models.BooleanField(
default=True, verbose_name=_("Evlilik Durumu"))
number_of_children = models.IntegerField(
verbose_name=_("Çocuk Sayısı"))
special_notes = models.TextField(
null=True, blank=True, verbose_name=_("Özel Notlar"))
registration_date = models.DateField(verbose_name=_("Kayıt Tarihi"))
foreign_language = models.ForeignKey(Language, on_delete=models.SET(
get_default_language), null=True, blank=True, verbose_name=_("Yabancı Dil"))
class Meta:
permissions = (
("list_staff", _("List Staff")),
)
ordering = ['name', 'surname']
def __unicode__(self):
return "%s %s" % (self.name, self.surname)
def save(self, *args, **kwargs):
self.name = self.name.title()
self.surname = self.surname.upper()
groups = []
for staff_type_object in self.staff_type.all():
group = Group.objects.get_or_create(name=staff_type_object.name)[0]
groups.append(group)
self.user.groups = groups
self.user.save()
super(Staff, self).save(*args, **kwargs)
views.py:
#login_required(login_url='/accounts/login/')
def personelkayit(request):
staffs = Staff.objects.values("user__username", "name", "surname", "uuid")
if request.method == 'GET':
staff_uuid = request.GET.get("staff_uuid")
if staff_uuid:
instance = get_object_or_404(Staff, uuid=staff_uuid)
form = StaffForm(instance=instance)
form.fields['username'].initial = instance.user.username
form.fields['username'].widget.attrs['readonly'] = True
else:
form = StaffForm()
return render(request, 'personelkayit.html', {'form': form, 'staffs':
staffs, 'staff_uuid': staff_uuid})
elif request.method == 'POST':
staff_uuid = request.GET.get("staff_uuid")
if staff_uuid:
instance = get_object_or_404(Staff, uuid=staff_uuid)
form = StaffForm(request.POST or None,
request.FILES or None, instance=instance)
else:
form = StaffForm(request.POST, request.FILES or None)
if form.is_valid():
password = form.cleaned_data.get("password")
re_password = form.cleaned_data.get("re_password")
staff = form.save(commit=False)
staff.user = user
staff.save()
form.save_m2m()
staff.save()
return redirect('/')
else:
return render(request, "personelkayit.html", {'form': form, 'staffs': staffs})
personelkayit.html:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load i18n %}
{% load staticfiles %}
{% block content %}
<br>
<table style="width: 100%">
<tr>
<td style="width: 4%"></td>
<td style="width: 92%">
<div class="block-area" id="basic">
<form role="form" method="get">
<select class="form-control input-sm m-b-10" name="staff_uuid">
<option value="" {% if not staff_uuid %} selected="selected" {% endif %} disabled="disabled">{% trans "Personel" %}</option>
{% for staff in staffs %}
<option {% if staff_uuid and staff.uuid|lower == staff_uuid|lower %}selected="selected"{% endif %} value="{{ staff.uuid }}">{{ staff.user__username }} - {{ staff.name }} {{ staff.surname }}</option>
{% endfor %}
</select>
<br />
<button type="submit" class="btn btn-sm btn-alt">{% trans "Personel Düzenle" %}</button>
<a class="btn btn-sm btn-alt" href="{% url 'personelkayit' %}">{% trans "Personel Ekle" %}</a>
<div class="modal-footer"></div>
</form>
</div>
<div class="block-area" id="basic">
<form role="form" enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form|crispy }}
<div >
<br>
<button style="background-color: #002266 !important; color: white !important" type="submit" class="btn btn-lg btn-alt"><span class="glyphicon glyphicon-floppy-disk
pull-left"></span>{% trans "Kaydet" %}</button>
</div>
</form>
<br>
</div>
</td>
<td style="width: 4%"></td>
</tr>
</table>
<script>
$( document ).ready(function() {
$( "select[name$='staff_uuid']" ).select2({
theme: "bootstrap"
});
$( "select[name$='subject']" ).select2({
theme: "bootstrap"
});
$( "select[name$='staff_type']" ).select2({
theme: "bootstrap"
});
});
</script>
{% endblock content %}
The reason this happens is because Django will render the string-representation of model objects in the form. A model is, by default rendered by the name of the type of the object, and the primary key. So a string like Subject object (...) is normal.
You can simply implement a __str__ (and for python-2.x a __unicode__) for the Subject model, to render it the way you specify:
class Subject(models.Model):
# …
def __str__(self):
return …

The view students.views.addgrregister didn't return an HttpResponse object. It returned None instead

ValueError at /students/addgrregister/
i am trying to add students in gr_register but its giving an error due to this error the code is not working i also upload the template (addgrregister.html) kndly tell me where is the issue in these pages
models.py
class gr_register(models.Model):
Gender_Choices = (
('M', 'Male'),
('FM', 'Female'),
)
Status_Choices = (
('P', 'Present'),
('FM', 'Left'),
)
gr_no = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
date_birth = models.DateField(null=True)
classes_A = models.ForeignKey(Classes, on_delete=models.CASCADE, related_name="classes_A", default=1, verbose_name="Class of Admission")
sections_A = models.ForeignKey(Sections, on_delete=models.CASCADE, related_name="sections_A", default=1, verbose_name="Section of Admission")
gender = models.CharField(max_length=10, choices=Gender_Choices)
classes_C = models.ForeignKey(Classes, on_delete=models.CASCADE, related_name="classes_C", verbose_name="Current Class")
sections_C = models.ForeignKey(Sections, on_delete=models.CASCADE, related_name="sections_C", verbose_name="Current Section")
address = models.CharField(max_length=100, null=True, verbose_name="Home Address")
area_code = models.ForeignKey(Area, on_delete=models.CASCADE, verbose_name="Area")
status = models.CharField(max_length=10, choices=Status_Choices, default='P')
class Meta:
ordering = ('gr_no',)
def __str__(self):
return self.first_name
views.py
from django.shortcuts import get_object_or_404, render, redirect
def addgrregister(request):
if request.method == 'POST':
form = gr_registerForm(request.POST)
if form.is_valid():
form.save()
return redirect('home')
else:
form = gr_registerForm()
return render(request, 'students/addgrregister.html', {'form': form})
forms.py
from django import forms
from django.forms import ModelChoiceField, ModelForm
from .models import *
class gr_registerForm(ModelForm):
classes_A = forms.ModelChoiceField(queryset=Classes.objects.all())
sections_A = forms.ModelChoiceField(queryset=Sections.objects.all())
classes_C = forms.ModelChoiceField(queryset=Classes.objects.all())
sections_C = forms.ModelChoiceField(queryset=Sections.objects.all())
area_code = forms.ModelChoiceField(queryset=Area.objects.all())
class Meta:
model = gr_register
fields = '__all__'
def init(self, *args, **kwargs):
forms.ModelForm.init(self, *args, **kwargs)
addgrregister.html
{% extends 'authenticate/base.html' %}
{% block content %}
<div class="container">
<h4 class="text-center">ADD GR_REGISTER</h4>
<hr/>
<form method="POST" action="{% url 'addgrregister' %}" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
<div class="form-group row">
<label for="id_{{ field.name }}" class="col-2 col-form-label">{{ field.label }}</label>
<div class="col-10">
{{ field }}
</div>
</div>
{% endfor %}
<button type="submit" class="btn btn-primary" name="button">Add GR_REGISTER</button>
</form>
<br/><br/>
</div>
{% endblock %}
There is nothing returned when form is not valid. I think you try like this:
def addgrregister(request):
form = gr_registerForm(request.POST or None) # it initates a form. If the request type is POST, then there will be a dict available with posted data in request.POST. If request is not POST, then the form will initiate with empty data.
if request.method == 'POST':
if form.is_valid(): # Form valid checks if the submitted form is valid or not. If not, it will store errors in the form. When that form is passed to template, it will show errors in html
form.save() # It will store data in DB
return redirect('home')
# when for is invalid, it will show the error in the form
return render(request, 'students/addgrregister.html', {'form': form})
Update
Show form errors in template:
{% for field in form %}
<div class="form-group row">
<label for="id_{{ field.name }}" class="col-2 col-form-label">{{ field.label }}</label>
<div class="col-10">
{{ field }}
{{ field.errors }} // <-- Updated here
</div>
</div>
{% endfor %}

Django: Assign current user as foreign key to comments model

I have been working thorough the DjangoGirls tutorial and was trying to improve on the section on adding comments to an application - TutorialExtensions
I have added the comments to a simple photo blog application but what I was attempting to do was replace the author = models.CharField(max_length=200) with an alternative that would store the current/logged-in user who was commenting on the photo instance and then allow me to display on the photo_detail template.
I thought I was close using author = models.ForeignKey(User, related_name='Commenter') but this through up an error:
NOT NULL constraint failed: timeline_comment.author_id
Here is my models.py consisiting of a Photo model and Comments model:
class Photo(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True)
image = ProcessedImageField(upload_to=upload_location,
null=True,
blank=False,
processors=[Transpose(), ResizeToFit(1000, 1000, False)],
format='JPEG',
options={'quality': 50},
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
description = models.TextField(max_length=1000)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
class Comment(models.Model):
post = models.ForeignKey('timeline.Photo', related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField(max_length=1000)
created_date = models.DateTimeField(default=timezone.now)
The related view:
def photo_detail(request, slug=None):
if not request.user.is_authenticated():
return HttpResponseRedirect("/accounts/login")
instance = get_object_or_404(Photo, slug=slug)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = instance
comment.save()
return redirect('timeline:detail', slug=instance.slug)
else:
form = CommentForm()
share_string = quote_plus(instance.description)
context = {
"title": instance.title,
"instance": instance,
"share_string": share_string,
"form": form,
}
return render(request, "photo_detail.html", context)
My forms.py:
class CommentForm(forms.ModelForm):
text = forms.CharField(widget=forms.Textarea, label='Leave a comment: ')
class Meta:
model = Comment
fields = [
"text",
]
Finally the template for the photo_detail view:
<div class="row">
<div class="col-md-12" id="comments">
<p>
{% if instance.comments.count == 0 %}
No Comments
{% elif instance.comments.count == 1 %}
{{ instance.comments.count }} Comment
{% else %}
{{ instance.comments.count }} Comments
{% endif %}
</p>
<hr style="margin-top: 10px;">
{% for comment in instance.comments.all %}
<div class="comment">
<div class="date pull-right">{{ comment.created_date | timesince }} Ago</div>
<strong>{{ comment.author }}</strong>
<p>{{ comment.text|linebreaks }}</p>
</div>
<hr>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
</div>
</div>
{% if user.is_superuser or user.is_authenticated %}
<div class="row">
<div class="col-md-12">
<form method="POST" class="comment-form" action=''>
{% csrf_token %}
{{ form | crispy }}
<button type="submit" class="comment-add btn btn-lg btn-purple">Add</button>
</form>
</div>
</div>
{% endif %}
Could anybody recommend the best approach for this? Any help would be very much appreciated! Thank You.
Using the ForeignKey is correct[1] - the missing piece is that you need to assign that in your view. After comment = form.save(commit=False) just add one line:
comment.author = request.user
and it will work.
[1] although you don't want the related_name as "Commenter" (because it refers to the way you access the comment from the user: the default is comment_set which makes more sense).

Categories

Resources