Django DateTimeField (19)

作者 : 慕源网 本文共7442个字,预计阅读时间需要19分钟 发布时间: 2021-11-22 共186人阅读

本文是Django教程的一部分,您可以点击Django教程系列查看所有教程。

Django DateTimeField (19)

Django DateTimeField 简介

对于 Django 而言,有几种字段类型可以通过这些字段类型注入日期和时间值以存储在模型上。这些字段之间的区别在于所存储数据的格式。有仅存储日期值的选项,甚至可以单独捕获时间值。根据日期时间字段,将捕获日期和时间值。用户将需要在注入存储值时放置日期和时间值。存储的值可以再次检索并显示在控制台上。

例如:10-07-2019 00:20

语法:

Name_of_field = models.DateTimeField(**options)

上述语法对应于 Django DateTimeField 示例。这里提到的第一列是所涉及的字段的名称。第一列将形成字段名称,旁边是 models.DateTimeField 扩展名。此扩展意味着从模型部分导出 DateTime 字段方法。所以模型的部分现在将导入 DateTimeField 方法来生成预期的字段。然后必须声明 DateTimeField() 方法的参数。这里的常用值是 null 参数,它提到需要是有效日期时间未到位的记录的值。下一个参数将是空白参数。

创建一个 Django DateTimeField

1. Models.py 文件的变化:

如语法部分所述,需要在 models.py 文件中声明日期时间字段。我们可以注意到,日期时间字段在模型中被声明为年龄字段。

(models.py)

from django.db import models
from django.contrib.auth.models import User
# Model variables
# Create your models here.
class Bride(models.Model):
DATE_TIME_name = models.CharField(max_length=200,null=True)
DATE_TIME_age = models.Date Time field_Example_Field(null=True)
DATE_TIME_DOB = models. DateTimeField (null=True)
DATE_TIME_thegai = models.CharField(max_length=200,null=True)
DATE_TIME_State = models.CharField(max_length=50,null=True)
DATE_TIME_District = models.CharField(max_length=50,null=True)
DATE_TIME_Address = models.TextField(null=True)
DATE_TIME_Phone = models.BigInteger(null=True)
DATE_TIME_profession = models.CharField(max_length=200,null=True)
DATE_TIME_salary = models.BigInteger(null=True)
DATE_TIME_Under_Graduation_Degree = models.CharField(max_length=200,null=True)
DATE_TIME_Under_Graduation_college = models.CharField(max_length=400,null=True)
DATE_TIME_Post_Graduation_Degree = models.CharField(max_length=200,null=True)
DATE_TIME_Post_Graduation_college = models.CharField(max_length=400,null=True)
DATE_TIME_Rasi = models.CharField(max_length=200,null=True)
DATE_TIME_Nakshatra = models.CharField(max_length=200,null=True)
def __str__(self):
return self.name

2. Settings.py 文件的变化:

确保所有值和数据库连接都在 settings.py 文档中设置得很好,以便可以灵活地启动企业执行。下面声明的中间件对象需要在 settings.py 文档中很好地声明,因为这些中心件负责软件的功能,即使是在处理 GET 和 PUT 消息时也是如此。此外,所使用的模板还需要塞满,以便模板处理在后台进行。

(Settings.py):

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ROOT_URLCONF = 'Matrimony.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [Template_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'render_dict_processors': [
'django.template.render_dict_processors.debug',
'django.template.render_dict_processors.request',
'django.contrib.auth.render_dict_processors.auth',
'django.contrib.messages.render_dict_processors.messages',
],
},
},
]

3. url.py 文件的变化:

媒体根和文档根变量需要在 url.py 文件中实例化,如下所示。url.py 文件的更改如下所述。

url.py:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from matrimony_pages import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$',views.Welcome_page,name='Welcome_page'),
url(r'Mainpage/',views.Main_page,name='Main_page'),
url(r'form/',views.form_view,name='form_view'),
url(r"signup/", views.Sign_up_request, name="register"),
url(r"login/", views.login_request, name="login"),
path(r'profile/<str:pk>/',views.DATE_TIME_page,name='profile'),
url(r'logout/',views.logout_request,name='logout'),
url(r'reg/',views.DATE_TIME_reg_user,name='reg'),
path(r'update/<str:pk>/',views.form_update,name='update'),
path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

4. 为表单创建一个视图:

提交时的日期时间字段值必须存储,检索时必须从数据库中提取。这可以通过为模型创建的对象来实现。下面给出的 views.py 部分解释了这样做的过程。

例如:views.py

@login_required
def DATE_TIME_page(request,pk):
render_dict2 = {}
Key_details = Bride.objects.get(id=pk)
DATE_TIME_name = Key_details.name
DATE_TIME_Age = Key_details.age
DATE_TIME_DOB = Key_details.DOB
DATE_TIME_Thegai = Key_details.thegai
DATE_TIME_state = Key_details.State
DATE_TIME_district = Key_details.District
DATE_TIME_Address = Key_details.Address
DATE_TIME_Phone = Key_details.Phone
DATE_TIME_Profession = Key_details.profession
DATE_TIME_Salary = Key_details.salary
DATE_TIME_UG = Key_details.Under_Graduation_Degree
DATE_TIME_UGC = Key_details.Under_Graduation_college
DATE_TIME_PG = Key_details.Post_Graduation_Degree
DATE_TIME_PGC = Key_details.Post_Graduation_college
DATE_TIME_UG = Key_details.Under_Graduation_Degree
DATE_TIME_UGC = Key_details.Under_Graduation_college
DATE_TIME_PG = Key_details.Post_Graduation_Degree
DATE_TIME_PGC = Key_details.Post_Graduation_college
DATE_TIME_Rasi = Key_details.Rasi
DATE_TIME_Nakshatra = Key_details.Nakshatra
render_dict2['Age'] = DATE_TIME_Age
render_dict2['DOB'] = DATE_TIME_DOB
render_dict2['name'] = DATE_TIME_name
render_dict2['thegai'] = DATE_TIME_Thegai
render_dict2['State'] = DATE_TIME_state
render_dict2['district'] = DATE_TIME_district
render_dict2['Address'] = DATE_TIME_Address
render_dict2['Phone'] = DATE_TIME_Phone
render_dict2['profession'] = DATE_TIME_Profession
render_dict2['Under_Graduation_Degree'] = DATE_TIME_UG
render_dict2['Under_Graduation_college'] = DATE_TIME_UGC
render_dict2['Post_Graduation_Degree'] = DATE_TIME_PG
render_dict2['Post_Graduation_college'] = DATE_TIME_PGC
render_dict2['Rasi'] = DATE_TIME_Rasi
render_dict2['Nakshatra'] = DATE_TIME_Nakshatra
print(Key_details.Creator)
print(render_dict2)
return render(request,'Profilepage.html',render_dict2)

4) 制定用于显示表单的 HTML 文件:必须对 HTML 页面进行相应的更改。

Profilepage.html

<!DOCTYPE html>
<html style="font-size: 16px;">
<head>
<title>Profile</title>
{% load static %}
<link rel="stylesheet" href="{% static 'admin/css/Formpage.css' %}" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1" />
<!-- Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="body">
<nav class='navbar'>
<div class='navbar_div'>
<a class="navbar" onclick="redirect2()" >Home </a>
<a class="navbar" onclick="redirect2()" >Contact</a>
<a class="navbar" onclick="redirect1()" >Profiles</a>
</div>
</nav>
<div class="formarea">
<br>
{% block content %}
{% if Date Time field %}
<img src="{{Date Time field.url}}" align="right" alt="Date Time field" class="img-thumbnail" style="max-height:200px">
{% else %}
<img src="{% static 'admin/img/default.png' %}" align="right" alt="Date Time field" class="img-thumbnail" style="max-height:200px">
{% endif%}
<br></br>
<h6><strong>Name : &nbsp&nbsp {{name}}</strong></h6>
<h6><strong>Age : &nbsp&nbsp {{Age}}</strong></h6>
<h6><strong>DOB : &nbsp&nbsp {{DOB}}</strong></h6>
<h6><strong>Thegai : &nbsp&nbsp {{thegai}}</strong></h6>
<h6><strong>State : &nbsp&nbsp {{State}}</strong></h6>
<h6><strong>district : &nbsp&nbsp {{district}}</strong></h6>
<h6><strong>Address : &nbsp&nbsp {{Address}}</strong></h6>
<h6><strong>Phone : &nbsp&nbsp {{Phone}}</strong></h6>
<h6><strong>profession : &nbsp&nbsp {{profession}}</strong></h6>
<h6><strong>Under Graduation Degree : &nbsp&nbsp {{Under_Graduation_Degree}}</strong></h6>
<h6><strong>Post_Graduation_Degree : &nbsp&nbsp {{Post_Graduation_Degree}}</strong></h6>
<h6><strong>Post_Graduation_college : &nbsp&nbsp {{Post_Graduation_college}}</strong></h6>
<h6><strong>Rasi : &nbsp&nbsp {{Rasi}}</strong></h6>
<h6><strong>Nakshatra : &nbsp&nbsp {{Nakshatra}}</strong></h6>
{% endblock content %}
</div>
<script>
function form1() {
window.location.href = "http://127.0.0.1:8000/form";
}
function redirect1() {
window.location.href = "http://127.0.0.1:8000/Mainpage";
}
function redirect2() {
window.location.href = "http://127.0.0.1:8000/";
}
function redirect3() {
window.location.href = "http://127.0.0.1:8000/profile";
}
</script>
</body>
</html>

输出:

Django DateTimeField (19)

Django DateTimeField (19)

结论

上面的文章展示了与 DateTimeField 功能相对应的各种细节。还讨论了如何使用日期时间字段、可以存储什么类型的值、字段的实际语法以及选择和存储值的现场示例的描述。还共享了所创建示例的输出快照以供参考。


慕源网 » Django DateTimeField (19)

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录