feat(projects): 新增Django综合案例示例代码及素材

This commit is contained in:
100gle
2022-08-10 16:15:39 +08:00
parent b4c2257308
commit 3bf65b2755
65 changed files with 1496 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class ViewConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'view'

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,8 @@
from django.urls import path
from .views import IndexView, index
urlpatterns = [
path("", index),
path("class/", IndexView.as_view()),
]

View File

@@ -0,0 +1,59 @@
from django.http import HttpResponse, HttpResponseNotAllowed
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def index(request):
"""index page"""
method = request.method
if method == "GET":
return HttpResponseNotAllowed(["POST"])
elif method == "POST":
return HttpResponse(f"You has got this page by POST method.")
@method_decorator(csrf_exempt, name="dispatch")
class IndexView(View):
template = """\
<p>You has got this page by {method} method, the following steps are: <br />
{content}
</p>
"""
def get(self, request):
"""index page"""
steps = [
"1. handle GET request",
"2. log request and other info",
"3. query something from database",
]
response = self.template.format(
method="GET",
content=r"<br />".join(steps),
)
return HttpResponse(response)
def post(self, request):
steps = [
"1. handle POST request",
"2. log request and other info",
"3. get form or parameters from request",
"4. parse form or parameters",
"5. query something from database",
"6. return response",
]
response = self.template.format(
method="POST",
content=r"<br />".join(steps),
)
return HttpResponse(response)