我試圖用Django顯示一個簡單的表單輸入框。我正在部署在亞馬遜AWS上。該網站在另一臺服務器(pythonanywhere)上正常工作,但AWS上存在主要問題。具體而言,輸入框未顯示。我使用的模板如下:表單輸入框不顯示
home.html的
{% extends 'lists/base.html' %}
{% block header_text %}Start a new To-Do list {% endblock %}
{% block form_action %}{% url 'new_list' %}{% endblock %}
base.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>To-Do lists</title>
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/base.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 jumbotron">
<div class="text-center">
<h1>{% block header_text %}{% endblock %}</h1>
<form method="POST" action="{% block form_action %}{% endblock %}">
{{ form.text }}
{% csrf_token %}
{% if form.errors %}
<div class = "form-group has-error">
<span class = "help-block">{{ form.text.errors }}</span>
</div>
{% endif %}
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
{% block table %}
{% endblock %}
</div>
</div>
</div>
</body>
</html>
models.py
from django.db import models
from django
.core.urlresolvers import reverse
class List(models.Model):
def get_absolute_url(self):
return reverse('view_list', args=[self.id])
# Create your models here.
class Item(models.Model):
text = models.TextField(default = '')
list = models.ForeignKey(List, default = None)
#list = models.ForeignKey(List , default=None)
forms.py
from django import forms
from lists.models import Item
EMPTY_ITEM_ERROR = "You can't have an empty list item"
class ItemForm(forms.models.ModelForm):
class Meta:
model = Item
fields = ('text',)
widgets ={
'text' : forms.fields.TextInput(attrs={
'placeholder': 'Enter a to-do item',
'class': 'form-control input-lg',
}),
}
error_messages = {
'text' : { 'required': EMPTY_ITEM_ERROR }
}
views.py
from django.shortcuts import redirect, render
from lists.models import Item, List
from django.core.exceptions import ValidationError
from lists.forms import ItemForm
from lists.models import Item, List
# Create your views here.
def home_page(request):
return render(request, 'lists/home.html', {'form': ItemForm()})
urls.py
from django.conf.urls import url
from lists import views
urlpatterns = [
url(r'^new$', views.new_list, name='new_list'),
url(r'^(\d+)/$', views.view_list, name='view_list'),
]
目前該網站顯示以下內容:
我推/拉整個項目的GitHub和每個站點之間的代碼是相同的,但我沒有看到爲什麼文本輸入不顯示,除非表單需要在Django中以某種方式初始化或怪誕AWS?
當比較兩個網站,沒有文本框中的一個不產生如下:
<input class="form-control input-lg" id="id_text" name="text" placeholder="Enter a to-do item" type="text" />
即使它應該,每base.html文件語法。
更新
全views.py(每個建議的評論)是:
from django.shortcuts import redirect, render
from lists.models import Item, List
from django.core.exceptions import ValidationError
from lists.forms import ItemForm
from lists.models import Item, List
# Create your views here.
def home_page(request):
return render(request, 'lists/home.html', {'form': ItemForm()})
def new_list(request):
form = ItemForm(data=request.POST)
if form.is_valid():
list_ = List.objects.create()
Item.objects.create(text=request.POST['text'], list=list_)
return redirect(list_)
else:
return render(request, 'lists/home.html', {"form": form})
def view_list(request, list_id):
list_ = List.objects.get(id=list_id)
form = ItemForm()
if request.method == 'POST':
form = ItemForm(data=request.POST)
if form.is_valid():
Item.objects.create(text=request.POST['text'], list=list_)
return redirect(list_)
return render(request, 'lists/list.html', {'list': list_, "form": form})
您是否嘗試過運行'./manage.py collectstatic'?這是我在模板問題時嘗試的第一件事,特別是在服務器上。 – elethan
'home_page'視圖沒有在您發佈的URL定義中的任何地方使用...? – solarissmoke
@elethan - 是的,我運行了collectstatic感謝。我能夠看到模板的變化,並收集靜態看到它們影響頁面,但表單更新沒有運氣。我注意到,在pythonanywhere中,當我對forms.py進行更改時,我必須在該網站上運行「重新加載」功能,我認爲這可能會重新加載服務器並查看窗體更改的影響?因爲如果我沒有這個改變就改變了forms.py,我也沒有看到那裏的改變。 – Jeeves