Работа с формами


Модуль forms.py

# coding: utf-8
from django import forms

class MyForm(forms.Form):
    name = forms.CharField(max_length=128)
    text = forms.CharField(widget=forms.Textarea)

Модуль views.py

# coding: utf-8
from django.shortcuts import render, render_to_response
from myforms.forms import MyForm
from django.http import HttpResponseRedirect

def my_name(request):
    title = 'Работа с формами'
    if request.method == 'POST':
        form = MyForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/forms/tranks/')
    else:
        form = MyForm()

    return render(request, 'forms.html', {'title': title, 'form': form})


def myredirect(request):
    title = 'Успешная отправка формы'
    return render_to_response('trunk.html', {'title': title})

Модуль urls.py

# coding: utf-8
from django.conf.urls import url
from myforms.views import my_name, myredirect

urlpatterns = [
    url(r'^$', my_name),
    url(r'^tranks/$', myredirect)
]

Шаблон forms.html

{% extends 'base.html' %}
{% load bootstrap3 %}
{% block content %}
<form action="/forms/" method="POST">
    {% csrf_token %}
    {% bootstrap_form form %}
    {% buttons %}
        <button type="submit" class="btn btn-primary">Отправить</button>
    {% endbuttons %}
</form>
{% endblock %}

Шаблон trunk.html

{% extends 'base.html' %}

blog comments powered by Disqus