I am using Django, but I am hooked on form and HTML.

Asked 2 years ago, Updated 2 years ago, 143 views

Django is a beginner who develops web apps while studying.
I'm creating a page where I type in the search criteria for travel sites, but when I try to create a form and reflect it in the template, I can't pull the template as well as the image below.
I tried and tried to fix the problem, but it didn't improve.
I would appreciate it if someone could teach mem(__)m

 from django import forms

class ConditionForm (forms.Form):
    # start = 'Fixed'

    goal=forms.CharField(label='Destination', max_length=30, required=True,)

    people=forms.ChoiceField(label='number of people', choices=PEOPLE,
                               widget=forms.Select, required=True,
                               help_text='*Required',)

    age=forms.CharField(label='age', required=True,
                          help_text='*Required',)

    sex=forms.ChoiceField(label='gender', choices=SEX,
                            widget=forms.Select, required=True,
                            help_text='*Required',)

    def clean_people():
        cleaned_data=super().clean_people()
        people = cleaned_data.get('people')
        if not people:
            raise forms.ValidationError('Please enter.')
        return cleaned_data

    defclean_age():
        cleaned_data=super().clean_age()
        people = cleaned_data.get('age')
        if not age:
            raise forms.ValidationError('Please enter.')
        return cleaned_data

    defclean_sex():
        cleaned_data=super().clean_sex()
        sex = cleaned_data.get('sex')
        if not Sex:
            raise forms.ValidationError('Please enter.')
        return cleaned_data
 from django.shortcuts import render, redirect
from django.http.response import HttpResponse
from.import forms
from django.template.response import TemplateResponse
from.forms import ConditionForm

def index(request):
    return TemplateResponse(request, 'travel/condition.html',
                        {'index':index},)

condition_form(request):
    form = ConditionForm (request.POST)
    if request.method == 'POST':
        if form.is_valid():
            form.save()

context = {'form':form}
return render(request, 'travel/condition.html', context)
<!DOCTYPE html>
<html>
<head>
    <title>
    {{ index}}<br>
    </title>
</head>
<body>
    <form action='/form/'method='POST'>
        {% csrf_token%}
        {{ form.as_p}}<br>
        <input type="submit" value="send">
    </form>
</body>
</html>

Current Output Page

python3 html django form

2022-09-30 21:46

1 Answers

def index(request):
    form = ConditionForm()
    return TemplateResponse(request, 'travel/condition.html', {'index':index', 'form':form})

Wouldn't it be necessary to pass the form as context to the return of the index method?
The current output page appears to be the result returned by the index method instead of condition_form.


2022-09-30 21:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.