从列表django中获取默认值
Get the default value from a list django
我有一个包含状态列表的网站,如何仅从提交表单时创建的列表中检索步骤 1 并将其存储到数据库中?
models.py
class Photo(models.Model):
STEP1 = "step 1"
STEP2 = "step 2"
STEP3 = "step 3"
STEP4 = "step 4"
STATUS = (
(STEP1, 'Received'),
(STEP2, 'Cleaning'),
(STEP3, 'Leak '),
(STEP4, 'Loss Pressure Test'),
)
Datetime = models.DateTimeField(auto_now_add=True)
serialno = models.TextField() # serialno stand for serial number
partno = models.TextField() # partno stand for part number
reception = models.TextField()
Customername = models.TextField()
def __str__(self):
return self.reception
forms.py
class AddForm(forms.Form):
reception = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter reception number'}))
partno = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter part number'}))
serialno = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter Serial Number'}))
Customername = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter customer name'}))
class meta:
model = Photo
fields = ('reception', 'partno', 'serialno', 'Customername')
views.py
def addPhoto(request):
msg = None
if request.method == 'POST':
form = AddForm(request.POST)
if form.is_valid():
Datetime = datetime.now()
reception = form.cleaned_data['reception']
partno = form.cleaned_data['partno']
serialno = form.cleaned_data['serialno']
Customername = form.cleaned_data['Customername']
# create a new MCO object with the form data
form = Photo(Datetime=Datetime, reception=reception, partno=partno, serialno=serialno, Customername=Customername)
form.save()
context = {'form': form}
return redirect('/gallery', context)
else:
msg = 'form is not valid'
else:
form = AddForm()
return render(request, 'photos/add.html', {'form': form,})
这是用户输入详细信息的页面:
这是显示详细信息的页面(在完成操作后,它应该显示用户提交表单时收到的第 1 步,我该怎么做?:
为此,您可以在数据库中创建一个字段调用状态,这样默认情况下该值将为 default = STEP1,如下所示:
class Photo(models.Model):
STEP1 = "Received"
STEP2 = "Cleaning"
STEP3 = "Leak"
STEP4 = "Loss Pressure Test"
STATUS = (
(STEP1, 'Received'),
(STEP2, 'Cleaning'),
(STEP3, 'Leak '),
(STEP4, 'Loss Pressure Test'),
)
Datetime = models.DateTimeField(auto_now_add=True)
#i add the status here so by default it is just "Received"
status = models.CharField(max_length=20,choices=STATUS,
default=STEP1)
serialno = models.TextField() # serialno stand for serial number
partno = models.TextField() # partno stand for part number
reception = models.TextField()
Customername = models.TextField()
def __str__(self):
return self.reception
之后
1) 运行 进行迁移并迁移
现在在你的模板中你可以调用“your_instance.status”.
你的另一个问题的答案:
我如何做到当用户编辑 table 时,完成的操作将显示第 2 步?
让我们做这样的事情。
forms.py:
from .models import Photo
class PhotoForm(forms.ModelForm):
class Meta:
model = Photo
fields = ['serialno','partno','reception','Customername']
views.py
from .forms import PhotoForm
from .models import Photo
from django.shortcuts import render,get_object_or_404,redirect
def editphoto(request,photo_id):
photo = get_object_or_404(Photo,pk=photo_id)
if request.method == 'POST':
form = PhotoForm(request.POST or None)
if form.is_valid():
form.save()
#we can do something like this to check that serial number has changed
if form.serialno != photo.serialno:
form.serialno = 'Cleaning'
form.save()
return redirect('***somewhere***')
else:
form = PhotoForm(instance=photo)
return render(request,'your-template',{'form':form})
但请注意,这里存在安全问题,我实际上并没有检查用户是否是该 post.so 的作者,基本上现在任何用户都可以编辑 post,就是这样 bad.to 避免这种情况,你可以在 Photo 和你的 views.py 中为 User(model) 创建一个外键,你可以检查是否只是做这样的事情:photo = get_object_or_404(Photo ,pk=photo_id,author_id=request.user.pk) 现在当用户不是 post 的作者时它会引发 Not Found Page,即它。
快乐编码。
我有一个包含状态列表的网站,如何仅从提交表单时创建的列表中检索步骤 1 并将其存储到数据库中?
models.py
class Photo(models.Model):
STEP1 = "step 1"
STEP2 = "step 2"
STEP3 = "step 3"
STEP4 = "step 4"
STATUS = (
(STEP1, 'Received'),
(STEP2, 'Cleaning'),
(STEP3, 'Leak '),
(STEP4, 'Loss Pressure Test'),
)
Datetime = models.DateTimeField(auto_now_add=True)
serialno = models.TextField() # serialno stand for serial number
partno = models.TextField() # partno stand for part number
reception = models.TextField()
Customername = models.TextField()
def __str__(self):
return self.reception
forms.py
class AddForm(forms.Form):
reception = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter reception number'}))
partno = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter part number'}))
serialno = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter Serial Number'}))
Customername = forms.CharField(label='',
widget=forms.TextInput(
attrs={"class": 'form-control', 'placeholder': 'Enter customer name'}))
class meta:
model = Photo
fields = ('reception', 'partno', 'serialno', 'Customername')
views.py
def addPhoto(request):
msg = None
if request.method == 'POST':
form = AddForm(request.POST)
if form.is_valid():
Datetime = datetime.now()
reception = form.cleaned_data['reception']
partno = form.cleaned_data['partno']
serialno = form.cleaned_data['serialno']
Customername = form.cleaned_data['Customername']
# create a new MCO object with the form data
form = Photo(Datetime=Datetime, reception=reception, partno=partno, serialno=serialno, Customername=Customername)
form.save()
context = {'form': form}
return redirect('/gallery', context)
else:
msg = 'form is not valid'
else:
form = AddForm()
return render(request, 'photos/add.html', {'form': form,})
这是用户输入详细信息的页面:
这是显示详细信息的页面(在完成操作后,它应该显示用户提交表单时收到的第 1 步,我该怎么做?:
为此,您可以在数据库中创建一个字段调用状态,这样默认情况下该值将为 default = STEP1,如下所示:
class Photo(models.Model):
STEP1 = "Received"
STEP2 = "Cleaning"
STEP3 = "Leak"
STEP4 = "Loss Pressure Test"
STATUS = (
(STEP1, 'Received'),
(STEP2, 'Cleaning'),
(STEP3, 'Leak '),
(STEP4, 'Loss Pressure Test'),
)
Datetime = models.DateTimeField(auto_now_add=True)
#i add the status here so by default it is just "Received"
status = models.CharField(max_length=20,choices=STATUS,
default=STEP1)
serialno = models.TextField() # serialno stand for serial number
partno = models.TextField() # partno stand for part number
reception = models.TextField()
Customername = models.TextField()
def __str__(self):
return self.reception
之后
1) 运行 进行迁移并迁移
现在在你的模板中你可以调用“your_instance.status”.
你的另一个问题的答案:
我如何做到当用户编辑 table 时,完成的操作将显示第 2 步?
让我们做这样的事情。
forms.py:
from .models import Photo
class PhotoForm(forms.ModelForm):
class Meta:
model = Photo
fields = ['serialno','partno','reception','Customername']
views.py
from .forms import PhotoForm
from .models import Photo
from django.shortcuts import render,get_object_or_404,redirect
def editphoto(request,photo_id):
photo = get_object_or_404(Photo,pk=photo_id)
if request.method == 'POST':
form = PhotoForm(request.POST or None)
if form.is_valid():
form.save()
#we can do something like this to check that serial number has changed
if form.serialno != photo.serialno:
form.serialno = 'Cleaning'
form.save()
return redirect('***somewhere***')
else:
form = PhotoForm(instance=photo)
return render(request,'your-template',{'form':form})
但请注意,这里存在安全问题,我实际上并没有检查用户是否是该 post.so 的作者,基本上现在任何用户都可以编辑 post,就是这样 bad.to 避免这种情况,你可以在 Photo 和你的 views.py 中为 User(model) 创建一个外键,你可以检查是否只是做这样的事情:photo = get_object_or_404(Photo ,pk=photo_id,author_id=request.user.pk) 现在当用户不是 post 的作者时它会引发 Not Found Page,即它。
快乐编码。