Kaynağa Gözat

创建了article_list页面的view,model,admin注册等,未做前端

Shellmiao 4 yıl önce
ebeveyn
işleme
df55b7bf9d

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/../../../../../:\YouKnowSyncFiles\Git\MyBlog\.idea/dataSources/
+/dataSources.local.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/

+ 2 - 7
MyBlog/MyBlog/settings.py

@@ -15,7 +15,6 @@ import os
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
-
 # Quick-start development settings - unsuitable for production
 # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
 
@@ -27,7 +26,6 @@ DEBUG = True
 
 ALLOWED_HOSTS = []
 
-
 # Application definition
 
 INSTALLED_APPS = [
@@ -37,6 +35,7 @@ INSTALLED_APPS = [
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    'article',
 ]
 
 MIDDLEWARE = [
@@ -54,7 +53,7 @@ ROOT_URLCONF = 'MyBlog.urls'
 TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
-        'DIRS': [],
+        'DIRS': [os.path.join(BASE_DIR, 'templates')],
         'APP_DIRS': True,
         'OPTIONS': {
             'context_processors': [
@@ -69,7 +68,6 @@ TEMPLATES = [
 
 WSGI_APPLICATION = 'MyBlog.wsgi.application'
 
-
 # Database
 # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
 
@@ -80,7 +78,6 @@ DATABASES = {
     }
 }
 
-
 # Password validation
 # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
 
@@ -99,7 +96,6 @@ AUTH_PASSWORD_VALIDATORS = [
     },
 ]
 
-
 # Internationalization
 # https://docs.djangoproject.com/en/2.0/topics/i18n/
 
@@ -113,7 +109,6 @@ USE_L10N = True
 
 USE_TZ = True
 
-
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/2.0/howto/static-files/
 

+ 2 - 1
MyBlog/MyBlog/urls.py

@@ -14,8 +14,9 @@ Including another URLconf
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
 from django.contrib import admin
-from django.urls import path
+from django.urls import path, include
 
 urlpatterns = [
     path('admin/', admin.site.urls),
+    path('article/', include('article.urls', namespace='article')),
 ]

+ 0 - 0
MyBlog/article/__init__.py


+ 4 - 0
MyBlog/article/admin.py

@@ -0,0 +1,4 @@
+from django.contrib import admin
+from .models import ArticlePost
+
+admin.site.register(ArticlePost)

+ 5 - 0
MyBlog/article/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class ArticleConfig(AppConfig):
+    name = 'article'

+ 33 - 0
MyBlog/article/migrations/0001_initial.py

@@ -0,0 +1,33 @@
+# Generated by Django 3.1.1 on 2021-01-11 13:02
+
+import datetime
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+from django.utils.timezone import utc
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='ArticlePost',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('title', models.CharField(max_length=100)),
+                ('body', models.TextField()),
+                ('created', models.DateTimeField(default=datetime.datetime(2021, 1, 11, 13, 2, 9, 738792, tzinfo=utc))),
+                ('updated', models.DateTimeField(auto_now=True)),
+                ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+            ],
+            options={
+                'ordering': ('-created',),
+            },
+        ),
+    ]

+ 25 - 0
MyBlog/article/migrations/0002_auto_20210111_2106.py

@@ -0,0 +1,25 @@
+# Generated by Django 3.1.1 on 2021-01-11 13:06
+
+import datetime
+from django.db import migrations, models
+from django.utils.timezone import utc
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('article', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='articlepost',
+            name='created',
+            field=models.DateTimeField(default=datetime.datetime(2021, 1, 11, 13, 6, 7, 993233, tzinfo=utc)),
+        ),
+        migrations.AlterField(
+            model_name='articlepost',
+            name='updated',
+            field=models.DateTimeField(default=datetime.datetime(2021, 1, 11, 13, 6, 7, 993233, tzinfo=utc)),
+        ),
+    ]

+ 24 - 0
MyBlog/article/migrations/0003_auto_20210111_2110.py

@@ -0,0 +1,24 @@
+# Generated by Django 3.1.1 on 2021-01-11 13:10
+
+from django.db import migrations, models
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('article', '0002_auto_20210111_2106'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='articlepost',
+            name='created',
+            field=models.DateTimeField(default=django.utils.timezone.now),
+        ),
+        migrations.AlterField(
+            model_name='articlepost',
+            name='updated',
+            field=models.DateTimeField(default=django.utils.timezone.now),
+        ),
+    ]

+ 0 - 0
MyBlog/article/migrations/__init__.py


+ 20 - 0
MyBlog/article/models.py

@@ -0,0 +1,20 @@
+from django.db import models
+# 导入内建的User模型
+from django.contrib.auth.models import User
+# timezone用于处理时间相关的事物
+from django.utils import timezone
+
+
+# 博客文章数据模型
+class ArticlePost(models.Model):
+    author = models.ForeignKey(User, on_delete=models.CASCADE)
+    title = models.CharField(max_length=100)
+    body = models.TextField()
+    created = models.DateTimeField(default=timezone.now) #使用timezone.now()时 进行数据迁移会,有警告
+    updated = models.DateTimeField(default=timezone.now)
+
+    class Meta:
+        ordering = ('-created',)
+
+    def __str__(self):
+        return self.title

+ 3 - 0
MyBlog/article/tests.py

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

+ 9 - 0
MyBlog/article/urls.py

@@ -0,0 +1,9 @@
+from django.urls import path
+from . import views
+
+app_name = 'article'
+
+urlpatterns = [
+    path('article_list/', views.article_list, name='article_list'),
+    path('', views.article_list, name='test')
+]

+ 10 - 0
MyBlog/article/views.py

@@ -0,0 +1,10 @@
+from django.shortcuts import render
+from django.http import HttpResponse
+from .models import ArticlePost
+
+
+# 视图函数
+def article_list(request):
+    articles = ArticlePost.objects.all()
+    context = {'articles': articles}
+    return render(request, 'article/list.html', context)

BIN
MyBlog/db.sqlite3


+ 3 - 0
MyBlog/templates/article/list.html

@@ -0,0 +1,3 @@
+{%for article in articles%}
+<p>{{article.title}}</p>
+{% endfor %}