我是Django的初学者,因此这可能是一个简单的问题。但我无法成功通过这个。
这是我的models.py
class Category(models.Model):
name = models.CharField(max_length=128)
abbr = models.CharField(max_length=5)
def __unicode__(self):
return self.name
class Fabric(models.Model):
name = models.CharField(max_length=128)
abbr = models.CharField(max_length=5)
def __unicode__(self):
return self.name
class Manufacturer(models.Model):
name = models.CharField(max_length=128)
location = models.CharField(max_length=128)
name_abbr = models.CharField(max_length=5, default=None)
loc_abbr = models.CharField(max_length=5, default=None)
def __unicode__(self):
return self.name
class Images(models.Model):
design_id = models.CharField(max_length=128)
file = models.ImageField(upload_to='images')
cost_price = models.FloatField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
fabric = models.ForeignKey(Fabric, on_delete=models.CASCADE)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
selling_price = models.FloatField()
aliveness = models.IntegerField()
date_added = models.DateTimeField(default=datetime.datetime.now)
set_cat = models.IntegerField()
set_cat_no = models.IntegerField()
set_cat_name = models.CharField(max_length=50, blank=True)
我正在建立一个包含布料设计的服装管理数据库系统。我的forms.py是
class ImagesForm(forms.ModelForm):
class Meta:
model = Images
fields = ('file','cost_price','set_cat_no','set_cat_name',)
我的views.py
@login_required
def uploadphoto(request):
context = RequestContext(request)
context_dict = {}
if request.method == 'POST':
form = ImagesForm(request.POST,request.FILES)
if form.is_valid():
image = form.save(commit=False)
image.save()
return render_to_response(request,'cms/upload-photo.html', {'upload_image': form})
else:
print form.errors
else:
form = ImagesForm()
context_dict = {'upload_image': form}
return render_to_response('cms/upload-photo.html',context_dict, context)
我的upload-photo.html是
{% block main %}
<form id="upload_form" method="post" action="/zoomtail/upload-photo/" enctype="multipart/form-data">
{% csrf_token %}
{{ upload_image }}
</form>
{% endblock %}
在这里,我将界面中的所有字段都塞满了。我如何在它们之间留出空格?在upload-photo.html
我只提到{{ upload_image }}
。如何操纵它以获得间隔良好的UI?
答案 0 :(得分:1)
好吧,所以你将表单传递给模板,但默认情况下这是将表单元素添加为列表。一个选项但可能不是唯一的选择是输入表单元素 init 方法并添加字段类属性。完成后,您可能希望将css脚本添加到添加的类中,因此您将能够访问表单的布局。
这是我的一段代码:
def __init__(self, *args, **kwargs):
kwargs.setdefault('label_suffix', '')
super(FileUploadForm, self).__init__(*args, **kwargs)
self.fields['file_source'].widget.attrs = {
'class': 'form-control s90 h28 custom_f_all',
}
希望它有所帮助。
祝你好运!答案 1 :(得分:0)