Created
April 9, 2019 05:41
-
-
Save Xifeng2009/7d737ed80aee954e0a0054c3db3a652c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In app/forms.py | |
from django import forms | |
from .models import Topic | |
class TopicForm(forms.ModelForm): | |
class Meta: | |
model = Topic | |
fields = ['title', 'text'] | |
labels = { | |
'title': 'Title: ', | |
'text': 'Text: ', | |
} | |
# In app/views.py | |
from django.http import HttpResponseRedirect | |
from django.core.urlresolvers import reverse | |
from .forms import TopicForm | |
def base4(request): | |
if request.method == 'GET': | |
form = TopicForm() | |
elif request.method == 'POST': | |
form = TopicForm(request.POST) | |
if form.is_valid(): | |
form.save() | |
return HttpResponseRedirect(reverse('main:base2')) | |
context = {'form': form} | |
return render(request, 'examples/base4.html', context) | |
# In templates | |
<form action="{% url 'main:base4' %}" method="post"> | |
{% csrf_token %} | |
{{ form.as_p }} | |
<button name="submit">Add Topic</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment