我需要使用Django Date-field从数据库中检索数据。尝试使用Datefield检索数据。我正在使用Django 2.0,Bootstrap 4,PostgreSQL。
#forms.py
from django import forms
class StatusReportForm(forms.Form):
created_date=forms.DateField(widget=forms.TextInput(attrs=
{
'class':'datepicker'
}))
#views.py
from django.shortcuts import render, get_object_or_404
# Create your views here.
from django.views.generic import DetailView
from .models import Statusreport
from .forms import StatusReportForm
def statusreport(request):
form = StatusReportForm()
context = {
form: form
}
return render(request, "statusreport.html", context)
class StatusReportDetailView(DetailView):
model = Statusreport
template_name = 'statusreport-detail.html'
def get_object(self):
instance = get_object_or_404(Statusreport, slug=self.kwargs['slug'])
return instance
#models.py
class Statusreport(models.Model):
created_date= models.DateField()
taskname = models.CharField(max_length=200)
slug = models.SlugField(null=True, blank=True)
def __str__(self):
return self.taskname
def get_absolute_url(self):
return reverse('statusreport:statusreport-detail', kwargs={'slug':
self.slug})
def pre_save_statusreport_receiver(instance, *args, **kwargs):
if not instance.slug:
instance.slug = instance.created_date
pre_save.connect(pre_save_statusreport_receiver, sender= Statusreport)
#statusreport.html
<main class="container">
<form>
<div class="row" style="padding-top: 100px">
<div class="col">
<label class = 'control-label' for="date">Select Date: </label>
<input data-date-format="dd/mm/yyyy" id="datepicker">
</br>
</br>
<a href="#" class="btn btn-info btn-md active" role="button"
aria-pressed="true">Get Reports</a>
</div>
</div>
</form>
</main>
<script type="text/javascript">
$('#datepicker').datepicker({
weekStart: 1,
daysOfWeekHighlighted: "6,0",
autoclose: true,
todayHighlight: true,
});
$('#datepicker').datepicker("setDate", new Date());
</script>
注意:我需要它像从那里单击“获取报告”按钮一样,需要将其移至http://localhost:8000/statusreport/ {{created_date}} /
但是我无法获得报告。
答案 0 :(得分:0)
<form method=POST action={% url 'get_posts_from'%}>
{% csrf_token %}
<div class="row" style="padding-top: 100px">
<div class="col">
<label class = 'control-label' for="date">Select Date: </label>
<input name='date-needed' data-date-format="dd/mm/yyyy" id="datepicker">
</br>
</br>
<a href="#" class="btn btn-info btn-md active" role="button"
aria-pressed="true">Get Reports</a>
</div>
</div>
</form>
首先,您必须使您的表单方法可以发布
然后将action
(即您的网址)添加到将管理发布数据的视图中
然后将csrf_token
添加到表单
那么您必须将name
赋予#datepicker
,以便在您查看时
您将获得request.POST["date"]
,这是您以
在urls.py
url(r'^(?P<date>\d{4}-\d{2}-\d{2})/$', views.get_posts_from, name='get_posts_from')
然后查看
def get_posts_from(request, date):
if request.method =="POST":
date = request.POST['date']
#here you put your logic the date can be either picked from
#the post or even trough the URL on his own
return HttpResponse("POSTS from",date)
答案 1 :(得分:0)
替换此行
instance = get_object_or_404(Statusreport, slug=self.kwargs['slug'])
与此
instance = get_object_or_404(Statusreport, created_date=self.kwargs['slug'])
并将created_date
作为参数传递给子弹