没有输入字段,只提交按钮出现django

时间:2015-05-29 14:47:28

标签: django python-3.x django-forms

我的输入字段没有出现,但我的提交按钮是,我没有收到错误。为什么我的浏览器中没有标签。另外,如果我想将ModelForm类与User模型中的外键一起使用,我是否必须将其导入forms.py?如果您还需要其他任何信息,请与我们联系。我被卡住了。

project = CMIRS app = error_tracking

search_incidents.html

{% extends "base.html" %}

{% block content %}
<br>
<p>Not only have you successfully logged in, you have also made it to the Search Incidents page!</p>
<br><br>

<form action="error_tracking.views.search_usernames" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

views.py

import datetime

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

from error_tracking.models import Incident
from error_tracking.forms import IncidentForm

@login_required
def search_incidents(request):
    incident_list = Incident.objects.filter(open = 'True').order_by('-incident_date_time_reported')
    return render(request, 'search_incidents.html', { 
        'incident_list' : incident_list,
        'user' : request.user
    })

def search_usernames(request):
    if request.method == 'POST':
            form = IncidentForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/')
    else:
        form = IncidentForm()
    return render_to_response('error_tracking/search_incidents.html', {'form': form})

forms.py

from django.forms import ModelForm
from error_tracking.models import Incident

class IncidentForm(ModelForm):
    class Meta:
        model = Incident
        fields = ['user_id']

models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Incident(models.Model):
       incident_id = models.AutoField(primary_key=True)
       incident_date_time_reported = models.DateTimeField('incident reported', default=timezone.now)
       user_id = models.ForeignKey(User)
       incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now)
       clinical_sample = models.BooleanField(default=True)
       number_of_clinical_samples = models.IntegerField(default=0)
       sample_id_numbers = models.TextField(max_length=1000)
       equipment_id = models.ForeignKey(Equipment)
       incident_category = models.ForeignKey(Incident_Category)
       technician = models.TextField(max_length=1000)
       description = models.TextField(max_length=1000)
       action_taken = models.TextField(max_length=1000)
       open = models.BooleanField(default=True)

       def __str__(self):
               return self.incident_id

       class Meta:
               verbose_name_plural="Incident"

error_tracking / urls.py

from django.conf.urls import include, url

urlpatterns = [
    url(r'^$', 'error_tracking.views.search_incidents'),
]

1 个答案:

答案 0 :(得分:0)

知道了! count需要在countResponse["odata.count"]下声明并传回search_incidents.html

<强> views.py

form