开发

  • 注册app

# news/apps.py
class NewsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'news'


# djangostu/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',


    #  module new / apps.py /   NewsConfig
     'news.apps.NewsConfig'
]






  • 编写url 和视图函数的对应关系

from news import views


# djangostu/urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
    # path('/a', include('news.urls')),
    # www.xxo.com/abcd , url 对应的函数
    path('abcd',views.index)
]


  • json HttpResponse
  • html render
def index(request):


    context = {
        'news_list': [
            {
                'title': "图雀写作工具推出了新的版本",
                "content": "随随便便就能写出一篇好教程,真的很神奇",
            },
            {
                "title": "图雀社区正式推出快速入门系列教程",
                "content": "一杯茶的功夫,让你快速上手,绝无担忧",
            },
        ],
        'name':'lusifer',
        'age':17778179517
    }
    print(type(context))
    jd= json.dumps(context)
    # return HttpResponse(jd,content_type='application/json')

    return render(request,'news/idx.html',json.loads(jd))

静态资源

  • root
    • app
      • static
        • js
        • css
        • image
        • plugins vue,antd