首页 > 解决方案 > 如何解决我的视图文件中的值错误

问题描述

在我看来,谁能帮我解决这个问题。我已经被困了很长时间了。任何帮助将不胜感激。

我的错误

System check identified no issues (0 silenced).
December 18, 2019 - 09:28:36
Django version 2.2.8, using settings 'Timesheet.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[18/Dec/2019 09:28:42] "GET / HTTP/1.1" 200 6027
Internal Server Error: /
Traceback (most recent call last):
  File "D:\Django\TimeSheetProject\morabu\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\Django\TimeSheetProject\morabu\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    "returned None instead." % (callback.__module__, view_name)
ValueError: The view morabu_timesheet.views.create_timesheet_view didn't return an HttpResponse object. It returned None instead.
[18/Dec/2019 09:29:02] "POST / HTTP/1.1" 500 65059

我的观点.py

而且我不知道我的方法是否正确,但我只想保存原始 HTML 表单中的数据,因为我必须使用 java 脚本进行一些前端验证。

from django.shortcuts import render
from .models import TimesheetDetails

# Create your views here.
def create_timesheet_view(request):
    if request.method=="POST":
        if(request.POST.get('dateToday') and request.POST.get('dayToday') and request.POST.get('startTime')
            and request.POST.get('endTime')and request.POST.get('breakTime')and request.POST.get('normalTime')
            and request.POST.get('overTime')and request.POST.get('holidayTime')and request.POST.get('weekType')
            and request.POST.get('attendance')and request.POST.get('content')):
            post = TimesheetDetails()
            post.date = request.POST.get('dateToday')
            post.day = request.POST.get('day_today')
            post.startTime = request.POST.get('startTime')
            post.endTime = request.POST.get('endTime')
            post.breakTime = request.POST.get('breakTime')
            post.normalTime = request.POST.get('normalTime')
            post.overTime = request.POST.get('overTime')
            post.holidayTime = request.POST.get('holidayTime')
            post.weekType = request.POST.get('weekType')
            post.attendance = request.POST.get('attendance')
            post.content = request.POST.get('content')
            if post.is_valid():
                try:    
                    post.save()
                    return render(request,'timesheet/view_timesheet.html')
                except :
                    pass
    else:
        return render(request,'timesheet/create_timesheet.html')
def view_timesheet(request):
    return render(request,"timesheet/view_timesheet.html")

我的模型.py

类 TimesheetDetails(models.Model):

 date = models.CharField(max_length = 10)
    day = models.CharField(max_length = 10)
    startTime = models.CharField(max_length =10)
    endTime = models.CharField(max_length =10)
    breakTime = models.CharField(max_length=3)
    normalTime = models.CharField(max_length=10)
    overTime = models.CharField(max_length = 10)
    holidayTime = models.CharField(max_length = 10)
    weekType = models.CharField( max_length = 10)
    attendance = models.CharField( max_length = 10)
    content = models.TextField( max_length = 300)

最后是我的 HTML 页面

<form action="" method="post" autocomplete="off">
        {% csrf_token %}  
        <h3 id="date_Today"></h3>
        <p id="dayToday" name="dayToday" class="hideMe"></p>
        <p id="dateToday" name="dateToday" class="hideMe"></p>
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    <label for="startTime">開始時間</label>
                    <input type="text" class="form-control" name="startTime" id="startTime" placeholder="(HH:mm)フォーマットに入力して下さい">
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label for="endTime">終業時間</label>
                    <input type="text" class="form-control" name="endTime" id="endTime" placeholder="(HH:mm)フォーマットに入力して下さい">
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label for="breakTime">休憩時間</label>
                    <input type="text" class="form-control" name="breakTime" id="breakTime" >
                </div>
            </div>
        </div> 
        
        <div class="row">
                <div class="col-sm-4">
                    <div class="form-group">
                        <label for="normalTime">時間内時間&lt;/label>
                        <input type="text" class="form-control" name="breakTime" id="normalTime" >
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="form-group">
                        <label for="extraTime">時間外時間&lt;/label>
                        <input type="text" class="form-control" name="extraTime" id="extraTime">
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="form-group">
                        <label for="holidayTime">休日時間</label>
                        <input type="text" class="form-control" name="holidayTime" id="holidayTime" >
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="weekType">区分は間違えないでしょうか?</label>
                        <select class="form-control" name="weekType" id="weekType">
                            <option value="平日">平日</option>
                            <option value="休日">休日</option>
                        </select>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="form-group">
                        <label for="reason">勤怠を選んでください!</label>
                        <select class="form-control" name="attendance" id="attendance">
                            <option value="欠勤">欠勤</option>
                            <option value="有給休暇">有給休暇</option>
                            <option value="振替休日">振替休日</option>
                            <option value="特別休暇">特別休暇</option>
                            <option value="残業">残業</option>
                            <option value="早退">早退</option>
                            <option value="遅刻">遅刻</option>
                            <option value="直行">直行</option>
                            <option value="直帰">直帰</option>
                            <option value="その他&quot;>その他&lt;/option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <label for="workContent">恐れ入りますが、仕事の内容を書いていただきませんでしょうか?&lt;/label>
                        <textarea class="form-control" name="workContent" id="workContent" rows="3"></textarea>
                    </div>
                </div>
            </div>       
        </div>
        <button type="submit" class="btn btn-success btn-block"><i class="fas fa-database"></i> 保存</button>
        
    </form>

标签: htmldjangopython-3.xdjango-views

解决方案


欢迎来到 SO :)

返回时您没有返回post.is_valid()HttpResponse False

from django.shortcuts import render
from .models import TimesheetDetails

# Create your views here.
def create_timesheet_view(request):
    if request.method=="POST":
        if(request.POST.get('dateToday') and request.POST.get('dayToday') and request.POST.get('startTime')
            and request.POST.get('endTime')and request.POST.get('breakTime')and request.POST.get('normalTime')
            and request.POST.get('overTime')and request.POST.get('holidayTime')and request.POST.get('weekType')
            and request.POST.get('attendance')and request.POST.get('content')):
            post = TimesheetDetails()
            post.date = request.POST.get('dateToday')
            post.day = request.POST.get('day_today')
            post.startTime = request.POST.get('startTime')
            post.endTime = request.POST.get('endTime')
            post.breakTime = request.POST.get('breakTime')
            post.normalTime = request.POST.get('normalTime')
            post.overTime = request.POST.get('overTime')
            post.holidayTime = request.POST.get('holidayTime')
            post.weekType = request.POST.get('weekType')
            post.attendance = request.POST.get('attendance')
            post.content = request.POST.get('content')
            if post.is_valid():
                try:    
                    post.save()
                    return render(request,'timesheet/view_timesheet.html')
                except :
                    pass
            else:
                return render(request,'timesheet/create_timesheet.html') #Adding this line

    else:
        return render(request,'timesheet/create_timesheet.html')
def view_timesheet(request):
    return render(request,"timesheet/view_timesheet.html")

推荐阅读