zoukankan      html  css  js  c++  java
  • python django传参

    http://192.168.137.3:8000/blog/?title=dd&body=sdsdss
    node2:/django/mysite/blog#cat views.py
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    # from django.shortcuts import render, render_to_response
    from .models import *
    # Create your views here.
    from django.http import HttpResponse
    from django.template import loader
    import MySQLdb
    
    def query(a,b):
        conn= MySQLdb.connect(
            host='localhost',
            port = 3306,
            user='root',
            passwd='1234567',
            db ='tlcb',
            )
        cur = conn.cursor()
        title=a
        body=b
        #a=cur.execute("select title,body, DATE_FORMAT(timestamp,'%Y-%m-%d %k.%i.%s') A from blog_blogpost where title= %s and body= %s" ,(title,body))
        #a=cur.execute("select title,body, DATE_FORMAT(timestamp,'%Y-%m-%d %k.%i.%s') A from blog_blogpost where title= 'dd' and body= 'ddd'")
        #a=cur.execute("select title,timestamp,body from blog_blogpost where title= '12121' and body= '31313'")
        a=cur.execute('select title,body, DATE_FORMAT(timestamp,%s)  from blog_blogpost where title= %s and body= %s' ,('%Y-%m-%d %k.%i.%s',title,body))
        print a
        info = cur.fetchall()
        print '--------------------------'
        print info
        print '--------------------------'
        return info
        cur.close()
        conn.close()
    
    def archive(req):
      print req
      print '---------------------'
      print req.COOKIES
      print '---------------------'
      print type(req)
      print req.GET
      print req.GET['title']
      print req.GET['body']
      title=req.GET['title']
      body=req.GET['body']
      param={'title':title,'body':body}  
      print param
    # get all blogpost objects
      posts =query(title,body)
      print posts
      print type(posts)
      #print blog_list
      template = loader.get_template('archive.html')
      context = {
      'posts':posts
      }
      print '------------------------------------------'
      print  HttpResponse(template.render(context, req))
      print '------------------------------------------'
      return HttpResponse(template.render(context, req))
    
    
    
    
    node2:/django/mysite/blog/templates#cat archive.html
    {% extends "base.html" %}  
    {% block content %}
          {% for post in posts %}
          <h2>{{  post.0 }}</h2>
          <p>{{ post.1 }}</p>
          <p>{{ post.2 }}</p>
          {% endfor %}
      {% endblock %}


    
                                        
    
  • 相关阅读:
    RPC、HTTP、IDL
    秋招拿了7个offer,分享一些反思和经验
    “功能测试”到“测试开发”升级之路
    Github最全买房租房攻略,火了!
    王垠:怎样尊重一个程序员?
    为什么曾经优秀的人突然变得平庸?
    你这么努力,为什么能力提升还这么慢?
    打工人到什么状态,就可以离职了?
    怎样才能迅速晋升?
    如何做个优秀的经理?
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349425.html
Copyright © 2011-2022 走看看