from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
#使用python2
@python_2_unicode_compatible
class Article(models.Model):
title = models.CharField('标题',max_length=50)
# ‘’ 内的字符会在 admin中添加时显示,默认显示变量名title
author = models.ForeignKey(Author)
content = models.TextField()
score = models.IntegerField() # 文章的打分
tags = models.ManyToManyField('Tag')
def __str__(self):
return self.title
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
def my_property(self):
return self.first_name + ' ' + self.last_name
my_property.short_description = "Full name of the person"
#这里的文本会作为文章标题的说明文本
full_name = property(my_property)
<title>add</title>
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="提交">
</form>
其中{% csrf_token %} 是提交表单必须的东东,现在还不理解
views:
def index(request):
if request.method == 'GET':
form = AddForms()
return render(request,'index.html',{'form':form})
else:
form = AddForms(request.POST)
if form.is_valid():
a = form.cleaned_data['a']
b = form.cleaned_data['b']
#获取表单中的值
c = int(a)+int(b)
return render(request,'add.html',{'result':c})
else:
HttpResponse('Values was invalided !')
forms:
class AddForms(forms.Form):
a = forms.IntegerField()
b = forms.IntegerField()