how to access forms field in django? - python

This is my forms:
class signup_form(forms.ModelForm):
bio = forms.TextInput()
class Meta:
model = User
fields = ['username',
'password',
'first_name',
'last_name',
'email',
'date_joined']
And This one is my template page:
urlpatterns = [
......
url(r'^profile/(?P<username>[\w\-]+)/$', user_profile, name='user_profile'),
]
And this is signup template page:
{% extends parent_template|default:"tick/base_tick.html" %}
{% block title %}
{{ block.super }} ---> Sign Up HERE!!
{% endblock %}
{% block content %}
<div>
<div>
<form action="{% url 'user_profile' username={{ form.username }} %}" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Create User</button>
</form>
</div>
</div>
{% endblock %}
As you can see in the 'action' part of the form i want to access to the 'username' field of 'form' but i can't and the Django get me some error.
What Should I do?
Edit: This is the Error

Value of a field is accessed by form.field_name.value. Use can update your code by below code
<form action="{% url 'user_profile' username=from.username.value %}" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Create User</button>
</form>

Related

exclude content field from crispy forms

forms.py
from django import forms
from tinymce import TinyMCE
from .models import Article
class TinyMCEWidget(TinyMCE):
def use_required_attribute(self, *args):
return False
class PostForm(forms.ModelForm):
content = forms.CharField(
widget=TinyMCEWidget(
attrs={'required': False, 'cols': 30, 'rows': 10}
)
)
class Meta:
model = Article
fields = ('title', 'major', 'semester', 'book', 'unit', 'content')
article_form.html
{% extends "base.html" %}
{% load static %}
{% load tailwind_filters %}
{% block title %}Create{% endblock title %}
{% block content %}
{{ form.media }}
<div class="row form-error">
<div class="column" id="content">
<form method="post" action='' enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<input class="button" type="submit" value="Save">
</form>
</div>
</div>
{% endblock %}
I using TinyMCE to implement a rich text editor.
when I reload the page it gives me this:
AttributeError at /article/new/
'CSSContainer' object has no attribute 'tinymce'
I just want to use crispy forms on all fields and exclude the content field from crispy forms.
I did some google and I found this
[simpleisbetterthancomplex.com]
then I modified my code to:
{% extends "base.html" %}
{% load static %}
{% load tailwind_filters %}
{% block title %}Create{% endblock title %}
{% block content %}
{{ form.media }}
<div class="row form-error">
<div class="column" id="content">
<form method="post" action='' enctype="multipart/form-data">
{% csrf_token %}
{{ form.title|as_crispy_field }}
{{ form.major|as_crispy_field }}
{{ form.semester|as_crispy_field }}
{{ form.book|as_crispy_field }}
{{ form.unit|as_crispy_field }}
{{ form.content }}
<input class="button" type="submit" value="Save">
</form>
</div>
</div>
{% endblock %}

List with checked checkboxes in Django

So, i want to get the checked checkboxes items ids as a list and show them on another page. But when i get to that specific page i get the value 'None' instead of the list of ids. What could go wrong? I tried some different versions from another questions already posted on the site, but the result was the same.
Here is the code:
models.py:
from django.db import models
class afirmatii(models.Model):
text = models.CharField(max_length = 250)
def __str__(self):
return self.text
views.py:
def exam(request):
if request.method == 'POST':
checks = request.POST.get('selected[]')
request.session['checks2'] = checks
context = {
'title' : 'Title1',
'aff': afirmatii.objects.order_by('id')
}
return render(request, 'pages/exam.html', context)
def result(request):
checks = request.session.get('checks2')
context = {
'title' : 'Title2',
'checks': checks
}
return render(request, 'pages/result.html', context)
exam.html:
{% extends "./base.html" %}
{% block content %}
<div class="text-break">
<form action="{% url 'result' %}" method="POST">
{% csrf_token %}
{% for q in aff %}
<div class="border mb-3 rounded-sm bg-light p-2">
<div class="custom-control custom-checkbox checkbox-info">
<input type="checkbox" class="custom-control-input" id="{{ q.id }}" name = "selected[]">
<label class="custom-control-label" for="{{ q.id }}" name = 'selected[]'> {{ q.text }} </label>
</div>
</div>
{% endfor %}
<button type="submit" class="btn btn-md btn-outline-info">Next</button>
</form>
</div>
{% endblock content %}
result.html:
{% extends "./base.html" %}
{% block content %}
<body>
<div class="text-center">
<p class="pb-5"> {{ checks }} </p><br>
<div class="row">
<div class="col">
Home
</div>
<div class="col">
Learn more
</div>
</div>
</div>
</body>
{% endblock content %}
You can try use FORMSETS A formsets is a layer of abstraction to work with multiple forms on the same page.
You can paint the list of questions according to records in the table and get ids (or other fields) of the marked answers
forms.py
class ExamForm(forms.Form):
checkbox = forms.BooleanField(required=False)
id = forms.CharField(widget=forms.HiddenInput)
text = forms.CharField(widget=forms.HiddenInput)
views.py
from django.shortcuts import render
from django.forms import formset_factory
from .forms import *
def exam(request):
aff = afirmatii.objects.order_by('id')
exam_formset = formset_factory(ExamForm, extra=0)
formset = exam_formset(initial=[{'id': x.id, 'text': x.text} for x in aff])
checks = []
if request.method == 'POST':
formset = exam_formset(request.POST)
if formset.is_valid():
for form in formset.forms:
if form.cleaned_data.get('checkbox', None):
checks.append(form.cleaned_data)
context = {
'formset': formset,
'checks': checks,
}
return render(request, 'pages/exam.html', context)
exam.html
{% if not checks %}
<h1>Exam:</h1>
<form action="{% url 'exam' %}" method="POST">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset.forms %}
<div class="form-group">
{{ form.checkbox }}
{{ form.text.value }}
{{ form.id }}
{{ form.text }}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% else %}
<h1>Results:</h1>
{% for check in checks %}
<p>{{ check.id }} {{ check.text }}</p>
{% endfor %}
{% endif %}

After posting content of my blog ni, they are shown in html format rather than pure text

I am using tinymce for my web blog that allows me to post my blogs, but as I post they are shown in rawHTML format like below:
this is my template
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block styler %}
{% endblock styler%}
{% block content %}
<div class="content-section">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend class="border-bottom pb-3 mb-3">Post blog</legend>
{{ form|crispy }}
</fieldset>
<button class="btn btn-outline-info" type="submit">Post</button>
</form>
</div>
{% endblock content %}
and my forms.py
class PostCreateForm(forms.ModelForm):
title = forms.CharField()
content = forms.CharField(
widget=TinyMCEWidget(
attrs={'required': False, 'cols': 30, 'rows': 10}
)
)
thumbnail = forms.ImageField()
class Meta:
model = Post
fields = ['title', 'content', 'thumbnail']
and associated views.py
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
form_class = PostCreateForm
help me how to solve this problem, I searched a bit and find out this could be due to #Html.Raw(#Model.LongDescription) but I don't know either where do I have to add that? and please check if you can do any improvement for my code.
thank you for your help.
Usually you just need to add safe tag: {{ content|safe }}
Or you can wrap html in autoescape tag:
{% autoescape off %}
{{ content }}
{% endautoescape %}

How to use django-summernote in templates

I have setup django-summernote on my project and everything is great, it works very good on admin , but i want to use it on my templates.
Note :
in django-summernote documentation they only explain how to use it if you have forms.py file but I dont
my main urls.py :
urlpatterns = [
path('games/', include('core.urls', namespace='core')),
path('summernote/', include('django_summernote.urls')),
]
my app(name=core) urls.py :
from django.urls import path
from . import views
app_name = 'core'
urlpatterns = [
path('new/', views.GameCreate.as_view(), name='game_new'),
path('<int:pk>/edit/', views.GameUpdate.as_view(), name='game_edit'),
]
my views.py :
class GameCreate(LoginRequiredMixin, CreateView):
model = Game
template_name = 'core/game_new.html'
fields = '__all__'
redirect_field_name = 'home'
class GameUpdate(LoginRequiredMixin, UpdateView):
model = Game
template_name = 'core/game_edit.html'
fields = '__all__'
my template file "game_new.html" :
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %} Add New Game {% endblock %}
{% block main %}
<section class="main-section">
<div class="container">
<h1>New Game</h1>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
<input type='submit' value="Save" />
</form>
</div>
</section>
{% endblock %}
my template file "game_edit.html":
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block title %} Game Info {% endblock %}
{% block main %}
<section class="main-section"></section>
<div class="container">
<h1>Edit Game Info</h1>
<form action="" method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Update" />
</form>
</div>
</section>
{% endblock %}
my Models.py :
from django.db import models
from django.urls import reverse
# Create your models here.
class Game(models.Model):
name = models.CharField(max_length=140)
developer = models.CharField(max_length=140)
game_trailer = models.CharField(max_length=300, default="No Trailer")
game_story = models.TextField(default='No Story')
Firstly in your template dont forgate to add this:
{{ form.media }} <<<------ To load all js and css attached
In the document your models you must herit summernote widget. Example:
from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget
class FormFromSomeModel(forms.ModelForm):
class Meta:
model = SomeModel
widgets = {
'foo': SummernoteWidget(),
'bar': SummernoteInplaceWidget(),
}

Custom Django FormWizard Steps with Templates

This is my working FormWizard that I made by following this and this
views.py
from django.shortcuts import render
from django.template import RequestContext
from django.http import HttpResponseRedirect
from formtools.wizard.views import SessionWizardView
# Create your views here.
def index(request):
return render(request, 'wizardApp/index.html')
class ContactWizard(SessionWizardView):
template_name = "wizardApp/contact_form.html"
def done(self, form_list, **kwargs):
process_form_data(form_list)
return HttpResponseRedirect('../home')
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
print(form_data[0]['subject'])
print(form_data[0]['info1'])
print(form_data[0]['info2'])
print(form_data[1]['sender'])
print(form_data[1]['info1'])
print(form_data[1]['info2'])
print(form_data[2]['message'])
print(form_data[2]['info1'])
print(form_data[2]['info2'])
return form_data
urls.py
from django.conf.urls import url
from wizardApp import views
from wizardApp.forms import ContactForm1, ContactForm2, ContactForm3
from wizardApp.views import ContactWizard
app_name = 'wizardApp'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^home/$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2, ContactForm3])),
]
forms.py
from django import forms
class ContactForm1(forms.Form):
subject = forms.CharField(max_length=100)
info1 = forms.CharField(max_length=100)
info2 = forms.CharField(max_length=100)
class ContactForm2(forms.Form):
sender = forms.EmailField()
info1 = forms.CharField(max_length=100)
info2 = forms.CharField(max_length=100)
class ContactForm3(forms.Form):
info1 = forms.CharField(max_length=100)
info2 = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
contact_form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
{{ wizard.form.media }}
</head>
<body>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="/contact/" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">first step</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">prev step</button>
{% endif %}
<input type="submit" value="submit"/>
</form>
</body>
</html>
I am having a lot of trouble understanding how customizing each step of the form works. There is very little help out there for this unfortunately. I saw this post about creating multiple templates and that kind of helps, but my main disconnect is on how i create those templates and how they are implemented in each step.
In a normal form i can do something like this
<form novalidate="novalidate" autocomplete="on" method="POST">
{% csrf_token %}
<div class="form-horizontal">
<div class="form-left">
{{form.first_name}}
{{form.first_name.errors}}
</div>
<div class="form-right">
{{form.last_name}}
{{form.last_name.errors}}
</div>
</div>
<div>
{{form.email}}
{{form.email.errors}}
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
How do i access each individual field? Where i can add in html and other bits to help with general styling? How should i go about making one of these for each steps? Should i basically be copy and pasting the html and everything into other "templates"? How do i call each template for each step?
Thanks!
Hope you figured it out. For the sake of anyone who runs into this, this is how I solved this. I replaced {{ wizard.form }} with a for loop to manually render the inputs:
<form action="/contact/" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{% for field in wizard.form %}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
<span class="message">{{ field.errors }}</span>
{% endfor %}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">first step</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">prev step</button>
{% endif %}
<input type="submit" value="submit"/>
You can create a template for each of your forms and then associate it to its corresponding form as described in the docs or if you want each form to use the same template, all you need to do is set your template_name attribute in your ContactWizard class:
class ContactWizard(SessionWizardView):
template_name = "contact_form.html"

Categories