Multiplayer blog project build process (c)

Bowen related interfaces

Functional Analysis

Create a blog application

 

model

#在post/models.py中
from django.db import models
from user.models import User
# Create your models here.
class Post(models.Model):
    class Meta:
        db_table = 'post'
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=256,null=False)
    postdate = models.DateTimeField(null=False)
    author = models.ForeignKey(User)#指定外键,migrate会生成author_id字段
    #Content access self.content instance, its content is self.content.content 

    DEF  __repr__ (Self):
         return  ' <Post {} {} {} {}> ' .format ( 
            self.id, self.title, self.author , self.content 
            # ibid self.id, self.title, self.author_id, self.content 
        ) 

    __str__ = __repr__ 

# MySQL table-one, one-to-many problem reference: https: //www.cnblogs .com / Camiluo / the p-/ 10615065.html 
class Content (models.Model):
     class Meta: 
        db_table = ' Content ' 
    # no primary key, automatically creates an auto-incremented primary key 
    post = models.OneToOneField (Post, to_field = id)# One, here there is a foreign key reference post primary key id, and can be omitted 
    Content = models.TextField (null = False) 

    DEF  __repr__ (Self):
         return  ' <the Content {} {}> ' . the format (self.post.id, self.content [: 20 is ]) 

    __str__ = __repr__

routing

Global Settings

#在blog/urls.py中
from django.conf.urls import include
urlpatterns = [
    url(r'^$', admin.site.urls),
    url(r'^admin/', admin.site.urls),
    url(r'^index/',index),
    # url(r'^testfor/',testfor),
    url(r'^user/',include('user.urls'))#多级路由
    url(r'^post/',include('post.urls' )) # Multistage routing 
]

# POST / the urls.py 
from django.conf.urls Import URL
 from .views Import Pub, GET, getAll # , testMiddle 


the urlpatterns = [ 
    URL (R & lt ' ^ Pub ' , Pub), 
    URL (R & lt ' ^ (\ + D) $ ' , get), # to get a parameter passed str type 
    URL (R & lt ' ^ $ ' , getAll), 
]

pub interface

#post/urls.py
from django.http import HttpRequest,HttpResponse,JsonResponse
from django.http import HttpResponseBadRequest,HttpResponseNotFound
from user.views import authenticate
from user.models import User
import simplejson
import datetime
from .models import Post,Content

# Create your views here.
@authenticate
def pub(request:HttpRequest):
    post = Post()
    content =The Content ()
     the try : 
        payload = simplejson.loads (request.body) 
        post.title = payload [ ' title ' ]
         # post.author = the User (ID = request.user.id) #user is authenticated at the time of injection 
        = the request.user post.author # User is authenticated at the time of implantation 
        post.postdate = datetime.datetime.now ( 
            datetime.timezone (the datetime.timedelta (hours =. 8 )) 
        ) 
        post.save () # Get a post ID 

        content.content = payload [ ' Content ' ] 
        content.post= post#

        content.save()

        return JsonResponse({
            'post_id':post.id
        })
    except Exception as e:
        print(e)
        return HttpResponseBadRequest()

get interface

to be continued. . .

 

Guess you like

Origin www.cnblogs.com/xiaoshayu520ly/p/11432545.html
Recommended