def home(request): username = '******' # try and catch to avoid not exists error # try and catch to avoid not mutiple value error loginer1 = People(name="Acer", major="CS") # add: People() -> obj.save() # delete: obj.get -> obj.delete() # update: obj = People.objects.get(name=""), obj.name = "", obj.save() password = '' dbuser = People.objects.filter(name=username) # order_by, exclude, all, if request.method == 'POST': # request has two methods and hence two dict, one is POST and another is GET loginer = People(name=request.POST.get("username")) username = loginer.name # <django.forms.widgets.PasswordInput object at 0x03AE1550> # password = forms.PasswordInput() myLoginForm = LoginForm(request.POST) request.session['username'] = username if myLoginForm.is_valid(): username = myLoginForm.clean_data['username'] # query with condition:People.filter() # else: myLoginForm = LoginForm() response = render_to_response('home.html', {'username': username}) # avoid the situation: 确认重新提交表单 response.set_cookie('last_connection', datetime.datetime.now()) response.set_cookie('username', datetime.datetime.now()) return response
def register(request): if request.POST.get('name'): postinfo = request.POST _name = postinfo.get('name') _major = postinfo.get('major') _age = postinfo.get('age') _grade = postinfo.get('grade') newuser = People(name=_name, major=_major, age=_age, grade=_grade) if newuser.is_valid(): newuser.save() return HttpResponse("Register Success") return render(request, 'register.html')
def first_try(request): person = People(name = 'Zhaoxiaoli',job='teacher') html_string = ''' <!DOCTYPE html> <html> <head> <title>demo</title> <link rel="stylesheet" type="text/css" href="http://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css"> </head> <body> <h1 class="ui center aligned icon header"> <i class="comments icon"></i> hello,{{person.name}} </h1> </body> </html> ''' # 创建模版 t = Template(html_string) #传递给模版的变量是特殊的Python字典,称为上下文(Context) c =Context({'person':person}) # render() 将给定的模版与给定的上下文字典组合起来, # 并返回带有该呈现文本的HttpResponse对象 web_page = t.render(c) # 返回 HttpResponse对象 需要引入HttpResponse return HttpResponse(web_page)
def first_try(request): person = People(name='spock', job='officer') html_string = ''' <html> <body> Hello,The {{person.job}} {{person.name}} </body> </html> ''' t = Template(html_string) c = Context({'person': person}) web_page = t.render(c) return HttpResponse(web_page)
def first_try(request): person = People(name='Spock', job='officer') html_string = ''' <html> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.4/semantic.css" media="screen" title="no title" charset="utf-8"> </head> <body> <h1 class="ui center aligned icon header"> <i class="hand spock icon"></i> Hello, {{ person.name }} </h1> </body> </html> ''' t = Template(html_string) c = Context({'person': person}) web_page = t.render(c) return HttpResponse(web_page)
def first_try(request): person = People(name='alxe', job='it') html_string = ''' <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>first_try</title> </head> <body> <h1>Hello</h1> <h3> {{ person.name }} </h3> </body> </html> ''' t = Template(html_string) c = Context({'person': person}) web_page = t.render(c) return HttpResponse(web_page)
def first_try(request): #由于没有数据,所以只好先创建一个数据,再使用数据 person = People(name='Spock', job='officer') html_string = ''' <html> <head> <meta charset="utf-8"> <title>firstapp</title> </head> <body> <h1> Hello, {{ person.name }} </h1> </body> </html> ''' t = Template(html_string) #将其变成模板 c = Context({'person': person}) #获取数据库信息生成上下文 web_page = t.render(c) #进行渲染 return HttpResponse(web_page)
def first_try(request): person = People(name="Spock", job="officer") #把Html和CSS放进Template html_string = ''' <html> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.4/semantic.css"> </head> <body> <h1 class="ui center aligned icon header"> <i class="hand spock icon"></i> Hello, {{ person.name }} </h1> </body> </html> ''' # {{}}指模板变量 t = Template(html_string) #将数据定义为模板 c = Context({'person': person}) #Context是把数据装进模板,做出一个上下文,接收一个字典参数,名称和对应的变量是什么。 web_page = t.render(c) #render方法把上下文,即从数据库取得的信息,渲染到模板里,并存储到web_page里 return HttpResponse(web_page) #把网页变成一个http的对象