parent
2256642abe
commit
aba3d37c1c
37 changed files with 596 additions and 29 deletions
@ -0,0 +1,25 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 17:30 |
||||
|
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0001_initial'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AddField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
field=models.DateField(auto_now_add=True, default=django.utils.timezone.now, verbose_name='创建时间'), |
||||
preserve_default=False, |
||||
), |
||||
migrations.AddField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
field=models.DateField(auto_now=True, verbose_name='修改时间'), |
||||
), |
||||
] |
@ -0,0 +1,23 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 17:33 |
||||
|
||||
from django.db import migrations, models |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0002_blog_createtime_blog_modifytime'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
field=models.DateTimeField(auto_now_add=True, verbose_name='创建时间'), |
||||
), |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
field=models.DateTimeField(auto_now=True, verbose_name='修改时间'), |
||||
), |
||||
] |
@ -0,0 +1,23 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 17:37 |
||||
|
||||
from django.db import migrations, models |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0003_alter_blog_createtime_alter_blog_modifytime'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
field=models.DateField(auto_now_add=True, verbose_name='创建时间'), |
||||
), |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
field=models.DateField(auto_now=True, verbose_name='修改时间'), |
||||
), |
||||
] |
@ -0,0 +1,24 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 17:40 |
||||
|
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0004_alter_blog_createtime_alter_blog_modifytime'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
field=models.DateField(auto_now_add=True, verbose_name=django.utils.timezone.now), |
||||
), |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
field=models.DateField(auto_now=True, verbose_name=django.utils.timezone.now), |
||||
), |
||||
] |
@ -0,0 +1,24 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 17:43 |
||||
|
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0005_alter_blog_createtime_alter_blog_modifytime'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
field=models.DateField(default=django.utils.timezone.now, verbose_name='创建时间'), |
||||
), |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
field=models.DateField(auto_now=True, verbose_name='修改时间'), |
||||
), |
||||
] |
@ -0,0 +1,24 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 17:44 |
||||
|
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0006_alter_blog_createtime_alter_blog_modifytime'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='创建时间'), |
||||
), |
||||
migrations.AlterField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
field=models.DateTimeField(auto_now=True, verbose_name='修改时间'), |
||||
), |
||||
] |
@ -0,0 +1,32 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 18:00 |
||||
|
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0007_alter_blog_createtime_alter_blog_modifytime'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.RemoveField( |
||||
model_name='blog', |
||||
name='createTime', |
||||
), |
||||
migrations.RemoveField( |
||||
model_name='blog', |
||||
name='modifyTime', |
||||
), |
||||
migrations.AddField( |
||||
model_name='blog', |
||||
name='add_date', |
||||
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='保存日期'), |
||||
), |
||||
migrations.AddField( |
||||
model_name='blog', |
||||
name='mod_date', |
||||
field=models.DateTimeField(auto_now=True, verbose_name='最后修改日期'), |
||||
), |
||||
] |
@ -0,0 +1,32 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-17 18:04 |
||||
|
||||
from django.db import migrations, models |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('blog', '0008_remove_blog_createtime_remove_blog_modifytime_and_more'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.RemoveField( |
||||
model_name='blog', |
||||
name='add_date', |
||||
), |
||||
migrations.RemoveField( |
||||
model_name='blog', |
||||
name='mod_date', |
||||
), |
||||
migrations.AddField( |
||||
model_name='blog', |
||||
name='create_time', |
||||
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='创建时间'), |
||||
), |
||||
migrations.AddField( |
||||
model_name='blog', |
||||
name='modify_time', |
||||
field=models.DateTimeField(auto_now=True, verbose_name='修改时间'), |
||||
), |
||||
] |
@ -1,12 +1,13 @@ |
||||
from django.urls import path |
||||
import blog.views as blog_views |
||||
from blog.views.blogListView import BlogListView |
||||
from blog.views.blogDetailView import BlogDetailView |
||||
import blog.view as blog_views |
||||
|
||||
urlpatterns = [ |
||||
# path('list/', blog_views.blog_list, name='list'), |
||||
# path('detail/<int:blog_id>/', blog_views.blog_detail, name='detail') |
||||
|
||||
path('list/', BlogListView.as_view(), ), |
||||
path('detail/<int:pk>/', BlogDetailView.as_view()) |
||||
path('detail/<int:pk>/', BlogDetailView.as_view()), |
||||
path('index/',blog_views.index) |
||||
] |
Binary file not shown.
@ -0,0 +1,3 @@ |
||||
from django.contrib import admin |
||||
|
||||
# Register your models here. |
@ -0,0 +1,6 @@ |
||||
from django.apps import AppConfig |
||||
|
||||
|
||||
class MyapiConfig(AppConfig): |
||||
default_auto_field = 'django.db.models.BigAutoField' |
||||
name = 'myapi' |
@ -0,0 +1,3 @@ |
||||
from django.db import models |
||||
|
||||
# Create your models here. |
@ -0,0 +1,16 @@ |
||||
from django.contrib.auth.models import User, Group |
||||
from rest_framework import serializers |
||||
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer): |
||||
class Meta: |
||||
model = User |
||||
fields = ['url', 'username', 'email', 'groups'] |
||||
|
||||
|
||||
class GroupSerializer(serializers.HyperlinkedModelSerializer): |
||||
class Meta: |
||||
model = Group |
||||
fields = ['url', 'name'] |
||||
|
||||
|
@ -0,0 +1,3 @@ |
||||
from django.test import TestCase |
||||
|
||||
# Create your tests here. |
@ -0,0 +1,13 @@ |
||||
from django.urls import path, include |
||||
from rest_framework import routers |
||||
from myapi import views |
||||
|
||||
|
||||
router = routers.DefaultRouter() |
||||
router.register(r'users', views.UserViewSet) |
||||
router.register(r'groups', views.GroupViewSet) |
||||
|
||||
urlpatterns = [ |
||||
path('', include(router.urls)), |
||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) |
||||
] |
@ -0,0 +1,16 @@ |
||||
from django.contrib.auth.models import User, Group |
||||
from rest_framework import viewsets |
||||
from rest_framework import permissions |
||||
from myapi.serializers import UserSerializer,GroupSerializer |
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet): |
||||
queryset = User.objects.all().order_by('date_joined') |
||||
serializer_class = UserSerializer |
||||
permissions_classes = [permissions.IsAuthenticated] |
||||
|
||||
|
||||
class GroupViewSet(viewsets.ModelViewSet): |
||||
queryset = Group.objects.all() |
||||
serializer_class = GroupSerializer |
||||
permissions_classes = [permissions.IsAuthenticated] |
@ -0,0 +1,5 @@ |
||||
from django.contrib import admin |
||||
from snippets.models import Snippet |
||||
|
||||
# Register your models here. |
||||
admin.site.register(Snippet) |
@ -0,0 +1,6 @@ |
||||
from django.apps import AppConfig |
||||
|
||||
|
||||
class SnippetsConfig(AppConfig): |
||||
default_auto_field = 'django.db.models.BigAutoField' |
||||
name = 'snippets' |
File diff suppressed because one or more lines are too long
@ -0,0 +1,23 @@ |
||||
from django.db import models |
||||
from pygments.lexers import get_all_lexers |
||||
from pygments.styles import get_all_styles |
||||
|
||||
LEXERS = [item for item in get_all_lexers() if item[1]] |
||||
LANGUAGE_CHOICES = sorted([(item[1][0],item[0]) for item in LEXERS]) |
||||
STYLE_CHOICES = sorted([(item,item) for item in get_all_styles()]) |
||||
|
||||
|
||||
class Snippet(models.Model): |
||||
created = models.DateTimeField(auto_now_add=True) |
||||
title = models.CharField(max_length=100,blank=True,default='') |
||||
code = models.TextField() |
||||
linenos = models.BooleanField(default=False) |
||||
language = models.CharField(choices=LANGUAGE_CHOICES,default='python',max_length=100) |
||||
style = models.CharField(choices=STYLE_CHOICES, default='friendly',max_length=100) |
||||
|
||||
def __str__(self): |
||||
return str(self.id) + ' ' + self.title + ' ' + self.code |
||||
|
||||
class Meta: |
||||
ordering = ['created'] |
||||
|
@ -0,0 +1,29 @@ |
||||
from rest_framework import serializers |
||||
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES |
||||
|
||||
|
||||
# class SnippetSerializer(serializers.Serializer): |
||||
# id = serializers.IntegerField(read_only=True) |
||||
# title = serializers.CharField(required=False, allow_blank=True, max_length=100) |
||||
# code = serializers.CharField(style={'base_template': 'textarea.html'}) |
||||
# linenos = serializers.BooleanField(required=False) |
||||
# language = serializers.ChoiceField(choices=LANGUAGE_CHOICES,default='python') |
||||
# style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly') |
||||
# |
||||
# def create(self, validated_data): |
||||
# return Snippet.objects.create(**validated_data) |
||||
# |
||||
# def update(self, instance, validated_data): |
||||
# instance.title = validated_data.get('title', instance.title) |
||||
# instance.code = validated_data.get('code', instance.code) |
||||
# instance.linenos = validated_data.get('linenos', instance.linenos) |
||||
# instance.language = validated_data.get('language', instance.language) |
||||
# instance.style = validated_data.get('style', instance.style) |
||||
# instance.save() |
||||
# return instance |
||||
|
||||
|
||||
class SnippetSerializer(serializers.ModelSerializer): |
||||
class Meta: |
||||
model = Snippet |
||||
fields = ['id','created','title','code','linenos','language','style'] |
@ -0,0 +1,3 @@ |
||||
from django.test import TestCase |
||||
|
||||
# Create your tests here. |
@ -0,0 +1,12 @@ |
||||
from django.urls import path |
||||
from rest_framework.urlpatterns import format_suffix_patterns |
||||
|
||||
from snippets.views import SnippetView,SnippetDetailView |
||||
|
||||
urlpatterns = [ |
||||
path('snippets/',SnippetView.as_view()), |
||||
path('snippet/<int:pk>/',SnippetDetailView.as_view()), |
||||
] |
||||
|
||||
|
||||
urlpatterns = format_suffix_patterns(urlpatterns) |
@ -0,0 +1,134 @@ |
||||
from rest_framework.response import Response |
||||
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_204_NO_CONTENT, HTTP_201_CREATED |
||||
from django.http import Http404 |
||||
from snippets.models import Snippet |
||||
from snippets.serializers import SnippetSerializer |
||||
from rest_framework.views import APIView |
||||
|
||||
# @csrf_exempt |
||||
# def snippet_list(request): |
||||
# if request.method == 'GET': |
||||
# snippets = Snippet.objects.all() |
||||
# serializer = SnippetSerializer(snippets,many=True) |
||||
# return JsonResponse(serializer.data,safe=False) |
||||
# |
||||
# elif request.method == 'POST': |
||||
# data = JSONParser().parse(request) |
||||
# serializer = SnippetSerializer(data=data) # 序列化成JSON格式 |
||||
# if serializer.is_valid(): |
||||
# serializer.save() |
||||
# return JsonResponse(serializer.data,status=HTTP_201_CREATED) |
||||
# return JsonResponse(serializer.errors, status=HTTP_400_BAD_REQUEST) |
||||
# |
||||
# @csrf_exempt |
||||
# def snippet_detail(request,pk): |
||||
# try: |
||||
# snippet = Snippet.objects.get(pk=pk) |
||||
# except Snippet.DoesNotExist: |
||||
# return HttpResponse(status=HTTP_400_BAD_REQUEST) |
||||
# |
||||
# if request.method == 'GET': |
||||
# serializer = SnippetSerializer(snippet) |
||||
# return JsonResponse(serializer.data) |
||||
# |
||||
# elif request.method == 'PUT': |
||||
# data = JSONParser().parse(request) |
||||
# serializer = SnippetSerializer(data=data) |
||||
# if serializer.is_valid(): |
||||
# serializer.save() |
||||
# return JsonResponse(serializer.data) |
||||
# return JsonResponse(serializer.errors,status=HTTP_400_BAD_REQUEST) |
||||
# |
||||
# elif request.method == 'DELETE': |
||||
# snippet.delete() |
||||
# return HttpResponse(status=HTTP_204_NO_CONTENT) |
||||
|
||||
|
||||
# @api_view(['GET', 'POST']) |
||||
# def snippet_list(request, format=None): |
||||
# if request.method == 'GET': |
||||
# snippets = Snippet.objects.all() |
||||
# serializer = SnippetSerializer(snippets,many=True) |
||||
# return Response(serializer.data) |
||||
# |
||||
# elif request.method == 'POST': |
||||
# # 序列化成JSON格式 |
||||
# serializer = SnippetSerializer(data=request.data) |
||||
# if serializer.is_valid(): |
||||
# serializer.save() |
||||
# return Response(serializer.data, status=HTTP_201_CREATED) |
||||
# return Response(serializer.errors, status=HTTP_400_BAD_REQUEST) |
||||
|
||||
# |
||||
# @api_view(['GET', 'PUT', 'DELETE']) |
||||
# def snippet_detail(request, pk, format=None): |
||||
# try: |
||||
# snippet = Snippet.objects.get(pk=pk) |
||||
# except Snippet.DoesNotExist: |
||||
# return Response(status=HTTP_400_BAD_REQUEST) |
||||
# |
||||
# if request.method == 'GET': |
||||
# serializer = SnippetSerializer(snippet) |
||||
# return Response(serializer.data) |
||||
# |
||||
# elif request.method == 'PUT': |
||||
# serializer = SnippetSerializer(snippet,data=request.data) |
||||
# if serializer.is_valid(): |
||||
# serializer.save() |
||||
# return Response(serializer.data) |
||||
# return Response(serializer.errors,status=HTTP_400_BAD_REQUEST) |
||||
# |
||||
# elif request.method == 'DELETE': |
||||
# snippet.delete() |
||||
# return Response(status=HTTP_204_NO_CONTENT) |
||||
|
||||
|
||||
|
||||
class SnippetView(APIView): |
||||
# 获取所有的snippet |
||||
def get(self, request, format=None): |
||||
snippets = Snippet.objects.all() |
||||
serializer = SnippetSerializer(snippets, many=True) |
||||
return Response(serializer.data) |
||||
|
||||
# 创建一个新的snippet |
||||
def post(self, request, format=None): |
||||
serializer = SnippetSerializer(data=request.data) |
||||
if serializer.is_valid(): |
||||
serializer.save() |
||||
return Response(serializer.data,status=HTTP_201_CREATED) |
||||
return Response(status=HTTP_400_BAD_REQUEST) |
||||
|
||||
|
||||
class SnippetDetailView(APIView): |
||||
def get_object(self, pk): |
||||
try: |
||||
return Snippet.objects.get(pk=pk) |
||||
except Snippet.DoesNotExist: |
||||
return Http404 |
||||
|
||||
# 查询某个snippet的数据 |
||||
def get(self,request,pk,format=None): |
||||
snippet = self.get_object(pk) |
||||
serializer = SnippetSerializer(snippet) |
||||
return Response(serializer.data) |
||||
|
||||
# 更新某个snippet的数据 |
||||
def put(self,request,pk,format=None): |
||||
snippet = self.get_object(pk) |
||||
serializer = SnippetSerializer(snippet,data=request.data) |
||||
if serializer.is_valid(): |
||||
serializer.save() |
||||
return Response(serializer.data) |
||||
return Response(status=HTTP_400_BAD_REQUEST) |
||||
|
||||
# 删除某一个snippet |
||||
def delete(self,request,pk,format=None): |
||||
snippet = self.get_object(pk) |
||||
snippet.delete() |
||||
return Response(status=HTTP_204_NO_CONTENT) |
||||
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 7.1 KiB |
@ -0,0 +1,40 @@ |
||||
{% load static %} |
||||
<!doctype html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="UTF-8"> |
||||
<meta name="viewport" |
||||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> |
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> |
||||
<title>Document</title> |
||||
<style> |
||||
h2 { |
||||
text-align: center; |
||||
} |
||||
|
||||
.center { |
||||
display: flex; |
||||
align-items: center; |
||||
justify-content: center; |
||||
} |
||||
|
||||
.info { |
||||
font-size: 20px; |
||||
font-weight: bold; |
||||
color: red; |
||||
} |
||||
|
||||
</style> |
||||
</head> |
||||
<body> |
||||
<h2>blog欢迎界面</h2> |
||||
<div class="center"> |
||||
<img src={% static 'images/apple.png' %}> |
||||
<ul> |
||||
{% for blog in blog_ %} |
||||
</ul> |
||||
</div> |
||||
|
||||
|
||||
</body> |
||||
</html> |
Loading…
Reference in new issue