'CharField on django is outputted as (<django.forms.fields.CharField object at XXXX>,)
I'd like to output the input field on the template, but it is outputted as
django.forms.fields.CharField object at XXXX
How can I fix this issue? I have tried many ways to fix it. But I could not solve the issues.
source codes occur the error
views.py
from django.shortcuts import render
from . import forms
def index(request):
form = forms.Page1(request.GET or None)
if form.is_valid():
message = 'データ検証に成功しました'
else:
message = 'データ検証に失敗しました'
content = {
'title': 'とあるウェブアプリ',
'form': form,
'message': message,
}
return render(request, 'forms.html', content)
forms.py
from django import forms
EMPTY_CHOICES = (
('', '-'*10),
)
GENDER_CHOICES = (
('man', '男'),
('woman', '女')
)
class Page1(forms.Form):
user_sir_name = forms.CharField(
label='姓',
max_length=20,
required=True,
widget=forms.TextInput(),
),
user_given_name = forms.CharField(
label='名',
max_length=20,
required=True,
widget=forms.TextInput(),
),
user_sex = forms.ChoiceField(
label='性別',
widget=forms.Select,
choices=EMPTY_CHOICES + GENDER_CHOICES,
required=True,
)
forms.html
{% extends "base.html" %}
{% block content %}
{{ message }}
<hr>
<form method="get" action="">
{{ form.errors.user_sir_name }}
<label>{{ form.user_sir_name.label }} {{ form.user_sir_name }}</label><br>
{{ form.errors.user_given_name }}
<label>{{ form.user_given_name.label }} {{ form.user_given_name }}</label><br>
{{ form.errors.user_sex }}
<label>{{ form.user_sex.label }} {{ form.user_sex }}</label><br>
<br>
<input type="submit" value="送信">
</form>
{% endblock %}
{% load static %}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="content-language" content="ja">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<!-- semantic.css -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'page1/css/style.css' %}">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.js"></script>
<!-- semantic.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.min.js"></script>
<title>とあるウェブアプリ</title>
<style>
</style>
</head>
<body>
<div class="ui inverted large borderless fixed fluid menu column">
<a href="/" class="header item"><h1>{{title}}</h1></a>
</div>
<--You can edit contents below: -->
<div class="ui container" style="min-height:100vh;margin-top: 200px">
{% block content %}
{% endblock %}
</div>
<div class="ui inverted stackable footer segment">
<div class="ui container center aligned">
<div class="ui horizontal inverted small divided link list">
<a class="item">© 2019 とあるウェブアプリ</a>
<a class="item">利用規約</a>
<a class="item">プライバシーポリシー</a>
</div>
</div>
</div>
</body>
</html>
version info
Windows10,Python 3.7.5,Django(3, 0, 2, 'final', 0)
Solution 1:[1]
Problem in comas after fields in you models, just remove it.
all code like a = 1,
python means as a = (1,)
Solution 2:[2]
just remove the (,) from forms.py file there is no need to give , between two fields
forms.py
class Page1(forms.Form):
user_sir_name = forms.CharField(
label='?',
max_length=20,
required=True,
widget=forms.TextInput(),
)
user_given_name = forms.CharField(
label='?',
max_length=20,
required=True,
widget=forms.TextInput(),
)
user_sex = forms.ChoiceField(
label='??',
widget=forms.Select,
choices=EMPTY_CHOICES + GENDER_CHOICES,
required=True,
)
Solution 3:[3]
I came across the same issue and found this answer helpful, simply use .cleaned_data
Ex: form.cleaned_data['my_form_field_name']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LinnTroll |
Solution 2 | Shrikant Sawarkar |
Solution 3 | shimii |