full_clean() missing 1 required positional argument: 'self' - html

I'm currently using django version 2.2.4 and trying to create an edit button that will update my models. When trying to save the updated value an TypeError occured which it stated that "full_clean() missing 1 required positional argument: 'self'". I can't seem to detect any error from my codes. Thanks in advance for helping me.
my views.py file
def lab_edit(request, pk, template_name='webapp/lab_edit.html'):
lab= get_object_or_404(Labs, pk=pk)
form = LabForm(request.POST or None, instance=Labs)
if request.method == "POST":
if form.is_valid():
form.save()
return redirect('lab')
return render(request, template_name, {'form':form})
my lab_edit.html file
<div class='container'>
<h2>EDIT LAB</h2>
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
my LabForm
class LabForm(forms.ModelForm):
class Meta:
model = Labs
fields = ('labcode', 'name','administrator')

Your LabForm gets as instance= the model class, not a model object. You should fix that by passing lab instead:
def lab_edit(request, pk, template_name='webapp/lab_edit.html'):
lab = get_object_or_404(Labs, pk=pk)
if request.method == 'POST':
form = LabForm(request.POST, instance=lab)
if form.is_valid():
form.save()
return redirect('lab')
else:
form = LabForm(instance=lab)
return render(request, template_name, {'form':form})
By passing a reference to the class , you have basically called full_clean on the class, hence the error.
Note that you should not use request.POST or None since an empty POST request can still be a valid POST request.

Related

Image is not editing after click on update button

I am going through a Django tutorial and My blog post image is not editing for edit post in my blog app. I use Django==3.1.2.
views.py
def edit_post(request, post_id):
post = BlogPost.objects.get(id=post_id)
if request.method != 'POST':
form = UpdatePost(request.POST or None, request.FILES or None, instance=post)
else:
form = UpdatePost(instance=post, data=request.POST)
if form.is_valid():
form.save()
return redirect('mains:posts')
context = {'post':post,'form':form}
return render(request, 'mains/edit_post.html', context)
forms.py
class UpdatePost(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['blog_post_title','blog_description','image']
edit_post.html
{% block content %}
<div class="container">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<button type="submit">Save Changes</button>
</form>
</div>
{% endblock content %}
The Problem
I am trying to edit my post in my blog app, Everything is working fine ( Blog title and Blog description is changing ) - when i change image it returns ( redirect ) fine BUT image not changing.
I am stuck with this problem and have no idea what is wrong.
What have i tried.
1). When i first create the view then i didn't add (request.POST or None, request.FILES or None), BUT
when i notice that, this may be effecting from editing then i added and it still not editing the image.
2). I have also changed template before BUT nothing works.
3). I have also changed form before BUT nothing workes.
Help me in this. I will really appreciate your Help. Thank you in advance !!
try this...
def edit_post(request, post_id):
post = BlogPost.objects.get(id=post_id)
if request.method != 'POST':
form = UpdatePost(request.POST or None, request.FILES or None, instance=post)
else:
form = UpdatePost(instance=post, data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return redirect('mains:posts')
context = {'post':post,'form':form}
return render(request, 'mains/edit_post.html', context)

Is it possible to loop a custom model form in django?

Is it possible to create multiple objects for a model in django by looping the same form in a for loop. I am using a custom model form.
My template is:
{% for query in queryset %}
<form method="POST" action="{% url 'academics' %}" style=" padding: 5%">
{% csrf_token %}
<input type="text" name="Student" class="form-control" id="id_Student"
value="{{query}}">
<input type="text" name="Subject" class="form-control" required id="id_Subject">
<input type="checkbox" name="Presence" id="id_Presence">
<button type="Submit" id="submit">Submit</button>
{% endfor %}
<button type="Submit" id="submit">Submit</button>
</form>
My models.py is:
class Attendance(models.Model):
Student = models.CharField(max_length=100, blank=False)
Hour = models.CharField(max_length=1, blank=False)
Subject = models.CharField(max_length=8, blank=False)
Date = models.DateTimeField(default=timezone.now)
Presence = models.BooleanField(default=False, blank=False)
def __str__(self):
return f'{self.Student}'
My views.py is:
def academics(request):
if request.user.is_staff:
form = forms.AttendanceForm()
context = {
'form': form,
'queryset': User.objects.filter(profile__Year='SY',profile__Department='CSE')
}
if request.method == "POST" :
form = forms.AttendanceForm(request.POST)
if form.is_valid():
student = request.POST.get('Student')
hour = request.POST.get('Hour')
subject = request.POST.get('Subject')
boolean = request.POST.get('Presence')
def bool(boolean):
if boolean == 'on':
return 'True'
else:
return 'False'
form = Attendance(Student=student,Hour=hour,Subject=subject,Presence=bool(boolean))
form.save()
return render(request, 'console/academics.html',context)
Currently i can create multiple objects, but with the same values of the last form. ie, the object is created with the values of last form. Here i have looped the form so that n number of forms will be generated for n queries with the name filled automatically in the first field. I know explaining this is little complex. Anyone can help?
I'm not entirely clear what you mean by "looping a form", but if you want the user to be able to enter a list of arbitrary length of similar sets of data, then what you want is a Formset or a ModelFormset. When it comes back, you validate all the data that he has submitted, and if it's all good then you iterate through it, usually creating or modifying multiple objects.
Due to reputation I'm unable to comment but I believe this is how you achieve your desired result. by using WHILE LOOP.
I myself have not much knowledge of python & Django but I guess this is the logic. Please correct me if I am wrong instead of down voting.
var = 0
n = 5
if request.method == "POST":
form = forms.AttendanceForm(request.POST)
if form.is_valid():
while var < n:
student = request.POST.get('Student')
hour = request.POST.get('Hour')
subject = request.POST.get('Subject')
boolean = request.POST.get('Presence')
def bool(boolean):
if boolean == 'on':
return 'True'
else:
return 'False'
form = Attendance(Student=student, Hour=hour,Subject=subject,Presence=bool(boolean))
form.save()
var += 1
return render(request, 'console/academics.html', context)

Invalid form when uploading file in Django

I need to upload file on a Django page, however, after following the official tutorial, I was not able to upload it, it always gives the error "invalid form", and when I tried to print out the error msg of the form, it says "This field is required".
One thing notable is: I have 2 forms on one page, one is this upload form and the other one is for filling out information. Not sure if this is the root cause.
I have tried all solutions provided on the Internet.
Template file:
<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="file" value="upload" name="sourcefile">
<button type="submit">Upload</button>
</form>
Forms.py:
from django import forms
from .models import SourceFile
class UploadFileForm(forms.ModelForm):
class Meta:
model = SourceFile
fields = ('file', 'title')
Models.py:
from django.db import models
# Create your models here.
class SourceFile(models.Model):
title = models.CharField(max_length=255, blank=True)
file = models.FileField(upload_to="media/")
Views.py
def model_form_upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
instance = SourceFile(file_field=request.FILES['file'])
instance.save()
return JsonResponse({'error': False, 'message': 'Uploaded Successfully!'})
else:
print("Invalid form")
# return JsonResponse({'error': True, 'errors': form.errors})
else:
form = UploadFileForm()
return render(request, 'source_validation.html', {'form': form})
Your template is wrong. Either use {{ form.as_p }} which should display a file input field because file is a field in your form. (so remove the <input type="file" ...>)
Or don't use it and manually add the <input> fields, but then you must use the correct names. Your form expects a "file" parameter, not a "sourcefile" parameter:
<input type="file" name="file">
Also, you're overcomplicating things in your view (even though your current code will work if you fix your template):
if form.is_valid():
form.save() # this will save your model
return redirect(...)

Django - How to make the topics, that you create public? Learning Log Project

The Problem
I'm trying to make a project, where you can make topics, that can be private or public to unauthenticated users. In every topic, you can then make several entries, applying to that topic. Now I'm trying to make a checkbox in my new_topic.html, where if you check it, it evaluates to True, if not, to False. But I'm having trouble with making the checkbox evaluate to True, when I check it. And for some reason, there is two checkbuttons in the page that applies to new_topic.html; one with a label, and one with just a box.
What I've tried
I've tried recreating the db.sqlite3 database. But that didn't work.
I've tried using request.POST.get('public', False) in my new_topic() function, when saving the new_topic variable.ยจ
The Code
My learning_logs/models.py looks like this:
from django.db import models
from django.contrib.auth.models import User
class Topic(models.Model):
"""A topic the user is learning about."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
public = models.BooleanField(default=False)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
"""Return a string representation of the model."""
return self.text
class Entry(models.Model):
"""Something specific learned about a topic."""
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
"""Return a string representation of the model."""
# Add an ellipsis ONLY if the entry,
# is more than 50 characters long.
if self.text > self.text[:50]:
return self.text[:50] + "..."
elif self.text <= self.text[:50]:
return self.text[:50]
My learning_logs\views.py looks like this:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect, Http404
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from .models import Topic, Entry
from .forms import TopicForm, EntryForm
def index(request):
"""The Home Page for Learning Log."""
return render(request, 'learning_logs/index.html')
def check_topic_owner(request, topic):
"""Checks if the topic requested, is requested by the owner.
Else return Http404.
"""
if topic.owner != request.user:
raise Http404
#login_required
def topics(request):
"""Show all topics."""
topics = Topic.objects.filter(owner=request.user).order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
#login_required
def topic(request, topic_id):
"""Show a single topic and all its entries."""
topic = get_object_or_404(Topic, id=topic_id)
# Make sure the Topic belongs to the current user.
check_topic_owner(request, topic)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
#login_required
def new_topic(request):
"""Add a new topic."""
if request.method != 'POST':
# No data submitted; create a blank form.
form = TopicForm()
else:
# POST data submitted; process data.
form = TopicForm(data=request.POST)
if form.is_valid():
new_topic = form.save(commit=False)
new_topic.owner = request.user
new_topic.save()
return HttpResponseRedirect(reverse('learning_logs:topics'))
context = {'form': form}
return render(request, 'learning_logs/new_topic.html', context)
#login_required
def new_entry(request, topic_id):
"""Add a new entry for the particular topic."""
topic = get_object_or_404(Topic, id=topic_id)
check_topic_owner(request, topic)
if request.method != 'POST':
# No data submitted; create a blank form.
form = EntryForm()
else:
# POST data submitted; process data.
form = EntryForm(data=request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.topic = topic
if new_entry.topic.owner == request.user:
new_entry.save()
else:
return Http404
return HttpResponseRedirect(reverse('learning_logs:topic',
args=[topic_id]))
context = {'topic': topic, 'form': form}
return render(request, 'learning_logs/new_entry.html', context)
#login_required
def edit_entry(request, entry_id):
"""Edit an existing entry."""
entry = get_object_or_404(Entry, id=entry_id)
topic = entry.topic
check_topic_owner(request, topic)
if request.method != 'POST':
# Initial request; pre-fill form with the current entry.
form = EntryForm(instance=entry)
else:
# POST data submitted; process data.
form = EntryForm(instance=entry, data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('learning_logs:topic',
args=[topic.id]))
context = {'entry': entry, 'topic': topic, 'form': form}
return render(request, 'learning_logs/edit_entry.html', context)
My learning_logs\forms.py looks like this:
from django import forms
from .models import Topic, Entry
class TopicForm(forms.ModelForm):
class Meta:
model = Topic
fields = ['text']
labels = {'text': ''}
class EntryForm(forms.ModelForm):
class Meta:
model = Entry
fields = ['text']
labels = {'text': ''}
widgets = {'text': forms.Textarea(attrs={'cols': 80})}
My learning_logs\templates\learning_logs\new_topic.html looks like this:
{% extends "learning_logs/base.html" %}
{% load bootstrap3 %}
{% block header %}
<h2>New topic:</h2>
{% endblock header %}
{% block content %}
<h1>Add a new topic:</h1>
<form action="{% url 'learning_logs:new_topic' %}" method='post'
class="form">
{% csrf_token %}
{% bootstrap_form form %}
<div class="form-check">
<input class="form-check-input" type="checkbox" value=True id="public">
<label class="form-check-label" for="public">
Make it public?
</label>
</input>
</div>
{% buttons %}
<button name="submit" class="btn btn-primary">Add Topic</button>
{% endbuttons %}
</form>
{% endblock content %}
I just can't seem to fix this error. Thanks in advance! Any help is appreciated.
You have:
<input class="form-check-input" type="checkbox" value=True id="public">
<label class="form-check-label" for="public">
Make it public?
</label>
</input> # <<< this closing tag is wrong
correct input tag :
<input class="form-check-input" type="checkbox" value=True id="public" />
So you'll have:
<label class="form-check-label" for="public">
Make it public?
</label>
<input class="form-check-input" type="checkbox" value=True id="public" />

Django Model Boolean Field needs to be initially unchecked. initial=False required=False is not working

Forms.py
class CheckPostedForm(forms.ModelForm):
posted = forms.BooleanField(required=False, initial=False)
Views.py
postform = CheckPostedForm(request.POST, instance=author)
if postform.is_valid():
postform.save()
else:
print 'Cannot save post form.'
Models.py
posted = models.CharField(max_length=2, blank=True, null=True)
class Meta:
model = TmpPlInvoice
exclude = ['net_amt', 'post_date', 'address', 'particulars', 'pos_code', 'acct', 'cust', 'voucher_id', 'voucher_date', 'post_date']
labels = {
'posted': 'Posted',
}
I want a checkbox which is initially unchecked and based on th value from DB i check/uncheck it. No matter what i do the form always comes out as checked="checked".
print postform gives this,
<tr><th><label for="id_posted">Posted:</label></th><td><input checked="checked" id="id_posted" name="posted" type="checkbox" value="n" required /></td></tr>
I have searched alot every where, the docs say that it should be initially false required=False if we want to apply conditions.
Detail
Views.py
def master_detail_posted(request):
if request.method == 'GET':
author = TmpPlInvoice.objects.get(id=1)
postform = CheckPostedForm(instance=author)
return render(request,'main.html' ,{'postform': postform})
if request.method == 'POST':
author = TmpPlInvoice.objects.get(id=1)
postform = CheckPostedForm(request.POST, instance=author)
print postform
if postform.is_valid():
logger.info('saving post form %s', postform.cleaned_data)
postform.save()
else:
logger.info('post form is not valid %s %s', postform.errors, request.POST)
return render(request,'main.html' ,{'postform': postform})
Template
<div> {{ postform }} </div>
You've hard coded the field in the template. Clearly, Django can't change HTML tags that you've written explicitly.
You need to use Django tags in your template:
<td>{{ form.posted }}</td>