程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed view - function based view Django

編輯:Python
A detail view is a view ( Logic ), Used to display specific instances of tables in the database and all necessary details . It is used to display multiple types of data on a single page or view , For example, the user's profile .Django  Provides exceptional support for detailed views , But let's examine how it is done manually through a function - based view . This article focuses on  Detail View  an , Which involves Django Forms、Django Models And so on .  For detailed views , We need a project with some models and multiple instances , They will be displayed . 

Django  Detail view —— Function based view

How to use examples to create and use detailed views Explanation . Consider a person named  geeksforgeeks  Project , It has a name called  geeks  Applications for . 
After you have a project and an application , Let's create a model , We will create an instance of it from our view . stay  geeks/models.py  in , 
#  Import standards from built-in Libraries  Django  Model
from django.db import models

#  Declare a “GeeksModel” The new model of
class GeeksModel(models.Model):

#  The model field
title = models.CharField(max_length = 200)
description = models.TextField()

#  Rename the instance of the model with the title name
def __str__(self):
return self.title

After creating this model , We need to run two commands to create a database for it . 
Python manage.py makemigrations
Python manage.py migrate
Now let's use  shell  Create some instances of this model , Run the form  bash,  Python manage.py shell Enter the following command
>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create(
 title="title1",
 description="description1").save()
>>> GeeksModel.objects.create(
 title="title2",
 description="description2").save()
>>> GeeksModel.objects.create(
 title="title3",
 description="description3").save()

Now we are ready for the back end . Verify that the has been removed from http://localhost:8000/admin/geeks/geeksmodel/  Create examples

about  detail_view, Some identification is required to get a specific instance of the model . Usually it is the only primary key , for example id. To specify this ID , We need to be in  urls.py  It's defined in . go to  geeks/urls.py, 
from django.urls import path

# importing views from views..py
from .views import detail_view

urlpatterns = [
path('<id>', detail_view ),
]

Let's create a view and template for it . stay  geeks/views.py  in ,
from django.shortcuts import render

#  Relative import of tables
from .models import GeeksModel

#  from  url  Pass on  id  attribute
def detail_view(request, id):
#  Initial data dictionary with field name as key
context ={}

#  Add dictionary during initialization
context[&quot;data&quot;] = GeeksModel.objects.get(id = id)

return render(request, &quot;detail_view.html&quot;, context)

stay templates/Detail_view.html Create a template in ,&nbsp;
<div class=&quot;main&quot;>

<!-- Specify fields to be displayed -->
{{ data.title }}<br/>
{{ data.description }}<br/>

</div>

Let's check http://localhost:8000/1


The detailed view works normally . You can also display the selected fields according to the type of use required in various forms . Usually not  id  Used to define detailed views , It is  slug.
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved