Django字段形式 [英] Django fields in form

查看:120
本文介绍了Django字段形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的初学者,因此这可能是一个简单的问题。但我无法成功地完成这项工作。

I am a beginner in Django, hence this might be a simple issue. But I'm not able to get past this successfully.

这是我的models.py

This is my 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是

I'm building an apparel management database system which contains cloth designs. My forms.py is

class ImagesForm(forms.ModelForm):

class Meta:
    model = Images
    fields = ('file','cost_price','set_cat_no','set_cat_name',)

我的views.py

My 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是

My upload-photo.html is

{% block main %}

<form id="upload_form" method="post" action="/zoomtail/upload-photo/" enctype="multipart/form-data">
{% csrf_token %}
{{ upload_image }}
</form>
{% endblock %}




  1. 问题这是当我转到/上传照片/我没有看到类别,面料和制造商的外键的下拉菜单。我已经读过它应该自动生成但我没有看到。

  1. The problem here is when I goto /upload-photo/ I don't see the drop-downs to the foreign keys to categories, fabric and manufacturers. I have read that it should be automatically generated but I don't see any.

selling_price 必须根据cost_price给定的增加百分比来计算输入表格。我该怎么做呢?默认情况下,服装的 aliveness 必须设置为1.如何执行此操作?

And selling_price has to be calculated based on the given percentage increase from the cost_price which has to be entered in the form. How do I do this? And aliveness of an apparel has to be set by default as 1. How to do this?

服装的 set-cat 字段如果属于一个集合则为1,如果属于某个目录则为2。如何获得一个单选按钮,询问是否将集合或目录以及数据库中的整数捕获?

set-cat field of an apparel is 1 if it belongs to a set and 2 if it belongs to a catalogue. How do I get a radio button asking whether set or catalogue and that to be captured as an integer in the database?

服装的design-id字段必须是一个字符串,其中包含类别,结构,制造商等的缩写,以及它所属的所有字段。我如何动态地这样做?

And design-id field of an apparel has to be a string which contains abbreviations of category, fabric, manufacturer, etc etc all the fields it belongs to. How do I do this dynamically?

我知道,这是一个很长的问题,但我是新手,从天开始,这些让我头疼。我将非常感谢那些回答这个问题的人。

I know, this is a very long question but I'm a newbie and these are literally giving me headache since days. I shall be very thankful to those who answer this.

推荐答案


  1. 我相信这个问题下拉列表是您从 ImageForm 中排除了字段。你有:

  1. I believe the issue with the dropdown is that you've excluded the fields from ImageForm. You have:

fields =('file','cost_price','set_cat_no','set_cat_name',)

但应该有:

fields =('file','cost_price ','set_cat_no','set_cat_name','category','fabric','manufacturer ,)`

如果不是你的数据库中有 Categories Fabric 制造商?如果您的表为空,则下拉列表将为空。如果数据库中有值,是否生成了HTML但标签值为空(即< option> {this is blank}< / option> )?在django中,您可以覆盖 __ str __ 函数以指定下拉选项如何标记

if that doesn't work, are there any options in your database for Categories, Fabric, and Manufacturer? If your tables are empty, the dropdown will be empty. If there are values in the database, is there HTML being generated but the label value is blank (i.e. <option>{this is blank}</option>)? In django, you can override the __str__ function to specify how the dropdown options get labeled

覆盖 __ str __ ,如下所示:

class Category(models.Model):

    name = models.CharField(max_length=128)
    abbr = models.CharField(max_length=5)

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name




  1. 您可以计算 selling_price 的值以及任何其他计算值块如果request.method =='POST'

  1. You can compute the value of selling_price and any other computed value in the block if request.method == 'POST'.

示例:

def uploadphoto(request):

    context = RequestContext(request)
    context_dict = {}

    if request.method == 'POST':

        form = ImagesForm(request.POST,request.FILES)
        #- Calculate value(s) here -#

        if form.is_valid():

            image = form.save(commit=False)
            image.save()`




  1. 请参阅此帖子在这里使用单选按钮

  2. 您可以在与#2相同的位置执行此操作

  1. Please see this post here for using radio buttons
  2. You would do this in the same place as #2 above

这篇关于Django字段形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆