返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
0_Python-Web-Django入门系列笔记

Python-Web-Django: Building the First Web App

View Rendering Edit views.py in the App directory from django.shortcuts import render from django.http import HttpResponse # Create your views here. def hello(request): return HttpResponse(Hello Django!) URL Resolution Configuration Edit urls

曦远 (DealiAxy)约 5 分钟
目录10 个章节

View Rendering

Edit views.py in the App directory

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def hello(request):
    return HttpResponse("Hello Django!")

URL Resolution Configuration

Edit urls.py: Add a URL resolution rule path('', views.hello) to urlpatterns

from django.contrib import admin
from django.urls import path
from hello_app import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.hello)
]

Open the Browser to Test

Building with Templates

Above we used django.http.HttpResponse() to output "Hello Django!". This approach mixes data with the view, which does not conform to Django's MVC philosophy. Next we will use an html template to build the App. In the previous article, we already configured the templates (templates) directory in settings.py. Now, create a home.html file in the templates directory.

Write the template file:

{% block main %}
    <h1>
        {{ home }}
    </h1>
{% endblock main %}

Define it in views.py

def home(request):
    context = {}
    context['home'] = "home"
    return render(request, 'home.html', context)

Configure URL resolution in urls.py

As mentioned earlier, this will not be repeated.

Browser test

About

To learn more interesting operations, please follow my WeChat official account: DealiAxy Every article is collected on my blog: blog.deali.cn

_EOF_

Python-Web-Django: Building the First Web App

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…