Using the Django framework for game site building

Source: Internet
Author: User
Tags domain name server root access

Full Project link Click to open link


In the previous article we used JavaScript and HTML5 to achieve the pinball game, and in this article we hope to implement the game site based on it, to achieve user registration login, game record, ranking display, micro-blog sharing and other functions.





Final effect:


2. Website building and server Building

2.1 Overall composition of the website

Site Building I use the Django framework.

2.1.1 Database design

User:

field

type

null

key

default

description

name

varchar

No

Primary

 

User id

password

varchar ()

no

 

 

user password

bestscore

Integer

No

 

0

User highest score

Name

Varchar (20)

No

Name of legal person

the corresponding models;


Class Player (models. Model):    name = models. Charfield (max_length=50, primary_key =true)    password = models. Charfield (max_length=50)    Bestscore = models. Positiveintegerfield ()    def __unicode__ (self):        return u '%s%s%s%s '% (Self.name, Self.password, Self.weibo , SELF.WEBOPW, Self.bestscore)

2.1.2 Website Design

Site structure diagram:

URL settings

(R ' ^$ ', Views.login), (R ' ^index/$ ', Views.index), (R '    ^ballgame/$ ', views.game),    (R ' ^signup/$ ', Views.signup),    (R ' ^signresult/$ ', Views.signresult),    (R ' ^login/$ ', Views.loginresult),    (R ' ^ Storescore/(? p<score>[\d]*)/$ ', Views.storescore),    (R ' ^logout/$ ', views.logout), (        R ' ^site_media/(? p<path>.*) $ ', ' Django.views.static.serve ', {' Document_root ': Settings. Static_path}),


The corresponding views:

Important: Login control, log in to the request after the session to indicate that you are logged in, at each URL request in the views in the judgment, if the session does not have this information, return to the login page. Logout is the deletion of the session information in the request.


Login result def loginresult (request): if Request.method = = "POST": username = Request.            post["username"] Try:user = Player.objects.get (name = username) except Objectdoesnotexist: Return Render_to_response ("login.html") passwd = Request. post["Password"] if passwd! = User.password:return Httpresponseredirect ("/") request.session[' u        ser_id '] = user.name; Return Httpresponseredirect ("/index/")//Login page def login (Request): If "user_id" in Request.session:return Httprespo Nseredirect ("/index/") Else:return render_to_response (' login.html ')//Registration page DEF signup (request): Return render_t         O_response (' signup.html ')//Logout def logout (request): Try:del request.session[' user_id '] except Keyerror:    Pass return render_to_response (' login.html ')//index page def index (Request)://Login with session control in request If "user_id" in request.session:res = Player.objects.order_by ('-besTscore ') [0:9] user = Player.objects.get (name=request.session["user_id"]) score = User.bestscore Count = 1 Ares = Player.objects.order_by ('-bestscore ') for p in ares:if p.name==request.session["user _id "]: Break else:count= count+1 return Render_to_response (' Index. HTML ', {"username": request.session["user_id"], "Bestplayer": Res, "score": Score, "Rank": Count}) Else:return Render_to_response (' login.html ')//Game page Def game (Request): If "user_id" in Request.session:return render_to_respon Se (' ballgame.html ', {"username": request.session["user_id"]}) Else:return Httpresponseredirect ("")//Register result page def si Gnresult (Request): if Request.method = = "POST": username = Request.            post["username"] try:existed = Player.objects.get (name = username) except Objectdoesnotexist: passwd = Request. post["Password"] p = Player (name = Username, password= passwd, bestscore=1) P.save () return Render_to_response ("signsucceed.html")        Return Httpresponseredirect ("/signup/")//Save score def storescore (request,score): If ' user_id ' in request.session: UserID = request.session[' user_id ' Try:user = Player.objects.get (name=userid) except OBJECTD        Oesnotexist:return httpresponseredirect ("") Oldscore = User.bestscore; Print type (oldscore) print type (score) score = long (score) if score > Oldscore:print " Yes "Bestscore = score User.bestscore = Score User.save () Else:bestsco Re = Oldscore return Render_to_response ("score.html", {"username": User.Name, "Thisscore": Score, "Bestscore": BESTSC Ore}) Else:return Httpresponseredirect ("")

2.1.3 Server Build-up

This website chooses to use Apache server, Apache server does not support Python by default. So the external module needs to be loaded. Choose to use MODULE_WSGI here.


Configuration steps:

A Download the compiled module_wsgi.so from the Web site and place it in the Apache installation directory/modules folder. Add the following to the Apache installation directory/conf/httpd.conf file that is loaded in the module.

LoadModule wsgi_module modules/mod_wsgi.sowsgiscriptalias/"F:/danjo/game/django.wsgi"//set/url corresponding file. Alias/site_media f:/danjo/game/static   //Set the directory for the/site_media URL. This site places all the static loading content in the folder. <directory/>    Options followsymlinks    allowoverride None    order Deny,allow    deny from allow here for set pair /root access rights </Directory> <directory "F:/danjo/game" >  //This directory is the directory where the Django project is located        order Deny,allow        Allow from all</directory>



Add files to the Django project directory. Django.wsgi.

Import Osimport sysimport django.core.handlers.wsgisys.path.append (R ' f:/danjo/game ') os.environ[' Django_settings_ MODULE '] = ' game.settings ' application = Django.core.handlers.wsgi.WSGIHandler ()


This will allow you to access our Django framework-built website in Apache.

3. Implementation of Weibo features

Weibo sharing features require the use of the Weibo API, which is chosen using the JavaScript API. The use of APIs requires the application to be registered on the Weibo open platform and to indicate site ownership. All need our website to be able to sign in the net. So need to register domain name, here I choose to use peanut shell dynamic Domain name server. and used its free domain name kkopq.xicp.net.

Once you've done these steps, it's easy to share with Weibo. Add the following on the page that requires Weibo:

    <script src= "http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=2556191936" type= "Text/javascript" charset= " Utf-8 "></script>  //This is a library of Weibo APIs that need to be added after registering the app to get Appkey.

Define the Share button:

  <p><button class= "btn btn-success btn-hg" style= "width:200px;height:50px" type= "button" id= "Wb_publish" > Share</wb:button></p> <script>            wb2.anywhere (function (W) {            w.widget.publish ({             ID: ') Wb_publish ',             default_text: ' I had gotten {score}} points in the Kkball game! Come to play with me at kkopq.xicp.net! Yes Yes [Yes]})        ;     </script>


4.html UI Design.

Web UI I was using the Flat UI framework.


Using the Django framework for game site building

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.