python manage.py runserve 9527
python manage.py runserve 0.0.0.0:9527
需要在```settings.py```配置```ALLOWED_HOSTS```
```ALLOWED_HOSTS = ['*']```表示外部所有ip都可以访问
Win cmd
命令行输入ipconfig
Mac 终端
命令行输入ifconfig
python manage.py startapp hello
第一步:在views.p
y文件写一个函数
from django.http import HttpResponse
def hello_world(request):
return HttpResponse('Hello Word!')
2. 第二部:在```urls.py```配置规则
```python
from hello.views import hello_world
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', hello_world)
]
-----
## URL的常用配置
#### ```path(route, view, name, **kwargs)```函数
* route:URL匹配规则
* view:视图函数
* name:路由的名称
* **keargs:其他参数
#### ```include(url, namespace)```函数
* 用于导入模块的```urls```列表
* urls:URL匹配规则列表
* namespace:命名空间
* 使用```path('hello/', include('hello.urls'))```导入模块url
-----
## URL的逆向解析
可以通过url的```name```名称,解析出URL链接
```reverse('name')```
输入URL
http://127.0.0.1:8000/aricle/05/
URL规则
path('article/<int:month>',article_list, name='article_list')
正则表达式规则
re_path(r'^article/(?P<month>0?[1-9]|1[012])/$',article_list, name='article_list')
视图编写
def article(request, month):
return HttpResponse('article:{}'.format(months))
输入URL
htttp://127.0.0.1:8000/serarch/?name=五月天
URL规则
path('search/', search, name='search')
视图编写:
def search(request):
name = request.GET.get('name', None)
模板修改settings.py
配置项目文件的TEMPLATES
的值为'DIRS': [BASE_DIR / 'templates']
templates
,需要进行模块安装,如下:settings.py
配置项目文件的INSTALLED_APPS
下添加模块名称hello
render_to_string(template_name,context=None,request=None,using=None)
template_name:模板名称context:模板的上下文对象(dict)request:请求对象using:模板引擎名称(如Jinja2)输入URL
htttp://127.0.0.1:8000/hello/render/str/
URL规则
path('render/str/', render_str, name='render_str'),
视图编写:
```python
def render_str(request):
"""render_to_string 函数的使用"""
templ_name = 'index.html'
html = render_to_string(template_name=templ_name)
return HttpResponse(html)
### render()函数
```render(request, template_name,context=None,content_type=None,status=None,using=None)```
输入URL
htttp://127.0.0.1:8000/hello/render/html/
URL规则
path('render/html/', render_html, name='render_html'),
视图编写:
def render_html(request):
"""render 函数的使用"""
return render(request, 'index.html')
输入URL
http://127.0.0.1:9527/hello/http/req/
URL规则
path('http/req/', http_request, name='http_request'),
视图编写:
from django.http import HttpResponse
def http_request(request):
"""请求练习"""
# 1. 请求方式
print(request.method)
# 2. 请求头信息
header = request.META
print(header)
ua = request.META.get('HTTP_USER_AGENT', None)
print(ua)
print(request.headers)
print(request.headers['User-Agent'])
print(request.headers['user-agent'])
# 3. 获取请求参数
name = request.GET.get('name', '')
print(name)
return HttpResponse('响应')
URL规则
path('http/json/', http_jsonresp, name='http_jsonresp'),
视图编辑:
from django.http import JsonResponse
def http_jsonresp(request):
"""Json响应练习"""
user_info = {
'name': '张三',
'age': 35
}
return JsonResponse(user_info)
URL规则
path('http/file/', http_fileresp, name='http_fileresp'),
视图编辑:
from django.http import FileResponse
def http_fileresp(request):
"""File响应练习"""
return FileResponse(open('myfile.png', 'rb'))
HttpResponseRedirect
重定向redirect()
快捷方式视图编辑:
def article_detail(request, article_id):
if article_id < 1000:
# return HttpResponseRedirect(reverse('no_data_404'))
return redirect('no_data_404') # 视图函数名称
return HttpResponse('文章{}的内容'.format(article_id))
重写内置的错误处理视图
settings配置
DEBUG=False
URL规则
handler500 = "my_appv.views.page_500"
视图编辑:
from django.http import HttpResponse
def page_500(request):
return HttpResponse('服务器正忙,请稍后重试!')
输入URL
htttp://127.0.0.1:8000/hello/home/
URL规则
path('home/', HomeView.as_view(), name='home')
视图类编辑:
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'home.html'
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。