I have view (CBV) with django-ckeditor on front-end side. Everything is working, but I can't validate text field (is mandatory), when I'm clicking "Post comment" button, I don't receive any message that this field is required.
Below is my code.
form.py
class Comment(forms.ModelForm):
text = forms.CharField(widget=CKEditorUploadingWidget(), label='', required=True)
class Meta:
model = Comment
form in template:
<form action="" method="POST">
{% csrf_token %}
{{ form.media }}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Post comment</button>
</form>
Related
Im on investigation Django framework and now trying to create custom procedure of resetting password.
I use this URL path
path('password_reset_confirm/<uidb64>/<token>',
PasswordResetConfirmView.as_view(
template_name='user_account/reset_password/password_reset_confirm.html',
success_url='/account/password_reset_complete/'),
name="password_reset_confirm"),
In template i use crispy form
<form class="login-form p-4 rounded" method="post">
{% csrf_token %}
<h1 class="h4 mb-4 font-weight-bold">Change your password</h1>
<p>Use the form below to change your password.</p>
{{ form|crispy }}
<button type="submit" value="Change">
Submit
</button>
</form>
So now this form do not showing in template, and if if type {{ form }} instead of {{ form.as_p }} or crispy form, that showing me None in form place.
This is my forms.
class CreateBooksForm(forms.ModelForm):
languages = forms.CharField(widget=forms.TextInput)
file = forms.FileField(widget=forms.FileInput(attrs={'accept':'application/pdf'}))
class Meta:
model = Book
fields = "name","languages", "about","image","file"
This is my view.So when I render to my update view template I get the empty form for languages and files but other are populated.
#login_required
def post_update(request,pk):
update = get_object_or_404(Book,pk=pk)
form = CreateBooksForm(request.POST or None ,request.FILES or None,instance=update)
if request.method == 'POST':
if form.is_valid():
post = form.save(commit=False)
languages = form.cleaned_data['languages']
post.save() # must be save before adding m2m
tag_list=[Language.objects.get_or_create(name=tag)[0] for tag in post.languages.lower().split()]
for tag in tag_list:
a = post.language.add(tag)
post.language.set = a
post.save()
messages.success(request,'Updated successfully!')
update_book.delay(post.pk)
context ={
'form':form
}
return render(request,'books/update.html',context)
my template.So this is simple django crispy template I have used.
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<img class="rounded-circle" src="/media/{{form.image.value}}" height="100px" width="150px">
{{ form|crispy }}
<button type="submit" class="btn btn-dark">Update Books</button>
</form>
You'll have to render each field manually to get the prepopulated field.
for example:
{% for field in form %}
{% if field.languages %}
<input type="name" id={{ field.languages.id_for_label }} value={{ field.languages.value }}/>
{% endif %}
{% endfor %}
for more details refer docs
I have multiple form that handle with one view.
When I want to show my form in index.html and specific the field,Such as {{form_1.some_field}} All help texts and fields name disappear!
When I use {{ form_1}} everything run correctly. What is the problem?
This is my files:
index.html
<form method="post" class="mos-rtl">
{% csrf_token %}
<div>
<h4 class="mos-rtl">Section 1</h4>
<p>{{ form_1.some_field }}</p>
</div>
<div>
<h4 class="mos-rtl">Section 2</h4>
{{ form_2.some_field }}
<button type="submit" >submit</button>
</div>
</form>
forms.py
class Form1(ModelForm):
class Meta:
model = Model1
fields = '__all__'
class Form2(ModelForm):
class Meta:
model = Model2
fields = '__all__'
Views.py
def my_view(request):
if request.method == "POST":
form_1 = Form1(request.POST)
form_2 = Form2(request.POST)
if form_1.is_valid() and form_2.is_valid():
new_record_1 = form_1.save(commit=False)
new_record_1.save()
new_record_2 = form_2.save(commit=False)
new_record_2.record_1 = new_record_1
new_record_2.save()
return redirect('administrator:view_admin_curriculum')
else:
form_1 = Form1(request.POST)
form_2 = Form2(request.POST)
template = 'index.html'
context = {'form_1': form_1, 'form_2': form_2}
return render(request, template, context)
{{ form }} calls form.__str__() method, which on the other hand calls form.as_table(). So, because of this {{ form }} and {{ form.as_table }} are rendered in a same fashion.
Form class also support different kinds of rendering methods, like as_table(), as_p(), as_ul() (This is how Form object should be rendered as a html). All these methods implementation are in BaseForm class, which represents parent class of Form. This is source code.
So, You should try like this:
<form method="post" class="mos-rtl">
{% csrf_token %}
<div>
<h4 class="mos-rtl">Section 1</h4>
<p>{{ form_1.some_field }} {{ form_1.some_field.help_text }}</p>
</div>
<div>
<h4 class="mos-rtl">Section 2</h4>
{{ form_2.some_field }} {{ form_2.some_field.help_text }}
<button type="submit" >submit</button>
</div>
</form>
If you are trying to render Form fields manually like you did, You should render help_text (manually also), which represents attribute of field.
Hi I'd like to know how can i add a glyphicon to the left of "input" in my code because I'm using django and I have to use forms.py you know.
My HTML code :
<form methode="post" action="/connexion" class="form-horizontal" role="form">
{% csrf_token %}
{{ form.as_p }}
<div>
<button type="submit" class="btn btn-info"><span class="glyphicon glyphicon-off">Connexion</button>
</div>
</form>
forms.py :
class ConnexionForm(forms.Form):
user = forms.CharField(label='', max_length=30, widget=forms.TextInput(attrs={'placeholder': "Nom d'utilisateur"}))
password = forms.CharField(label='', widget=forms.PasswordInput)
You can just render fields manually instead of rendering the whole form, like:
<form ...>
...
<i class="glyphicon..."></> {{ form.field_name }}
...
</form>
See the docs.
I'm trying to create a button on the framework django that let me delete my own article on my blog. I tried to create a code for this functionality, but it doesn't work.
views.py
if(request.GET.get('delete')): #if the button is clicked
delete_article = get_object_or_404(Article, id=id)
if request.POST:
form = DeleteNewForm(request.POST, request.FILES)
if form.is_valid():
delete_article.delete()
return HttpResponseRedirect("/")
template.html
<form enctype='multipart/form-data' action="." method="post" class="form" autocomplete="off" autocorrect="off">
{% csrf_token %}
<div class="form-group">TITRE
{{ form.title.errors }}
{{ form.title }}
</div>
<div class="form-group">CONTENU
{{ form.media }}
{{ form.contenu.errors }}
{{ form.contenu }}
</div>
<div class="form-group">
{{ form.image.errors }}
{{ form.image }}
</div>
<input type="submit" class="btn btn-default" value="Edit Article"/>
<input type="submit" class="btn btn-default" value="Delete Article" name="delete">
</form>
Here is what happen when I submit the form : I am not redirected on the index as it should redirect me and the article has not been deleted.
Is there a problem which don't let me delete my article in these lines?
I have no idea what you're doing in your views or in your template. But if I want to delete something, I just define a separate view for that.
# views.py
from django.views.generic.base import View
class DeleteView(View):
def post(self, request *args, **kwargs):
article = get_object_or_404(id=request.POST['article_id'])
# perform some validation,
# like can this user delete this article or not, etc.
# if validation is successful, delete the article
article.delete()
return HttpResponseRedirect('/')
Your template should be like this:
<form action="/url/to/delete/view/" method="POST">
{% csrf_token %}
<input type="hidden" value="{{ article.id }}">
<input type="submit" value="Delete">
</form>