Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以表单形式传递数据库值(django)_Python_Django - Fatal编程技术网

Python 以表单形式传递数据库值(django)

Python 以表单形式传递数据库值(django),python,django,Python,Django,我的表单.py: class BannerForm(forms.ModelForm): name = forms.CharField(max_length=32) #Affiliazione = forms.CharField(disabled = True, initial='red') #in original question #affiliation = forms.ModelChoiceField(Affiliation.objects.all(), #

我的
表单.py

class BannerForm(forms.ModelForm):
    name = forms.CharField(max_length=32)
    #Affiliazione = forms.CharField(disabled = True, initial='red') #in original question
    #affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    #widget=forms.HiddenInput(), initial=Affiliation.objects.get(id=1))  #in original question
    Affiliazione = forms.CharField(disabled = True, required=False) #added after first answer
    affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    widget=forms.HiddenInput()) #added after first answer
class Options(models.Model):
    new_affiliation = models.ForeignKey('Affiliation')

class Affiliation(models.Model):
    name = models.CharField(max_length=32, unique=True)
    def __str__(self):
        return self.name

class Banner(models.Model):
    name = models.CharField(max_length=32, unique=True)
    affiliation = models.ForeignKey(Affiliation)
def add_banner(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = BannerForm(request.POST)
        print('form is post') #control
        # Have we been provided with a valid form?
        if form.is_valid():
            print('form is valid') #control
            # Save the new banner to the database.
            banner = form.save(commit=False)
            #some irrilevant code here
            form.save(commit=True)
            print('form salvato') #control
            # Now call the homepage() view.
            # The user will be shown the homepage.
            return homepage(request)
        else:
            # The supplied form contained errors - just print them to the terminal
            print (form.errors)
    else:
        # If the request was not a POST, display the form to enter details
        #form = BannerForm(request.POST) #in original question
        #initial_value = 'red' #added after first answer
        #init = Affiliation.objects.get(id=1) #added after first answer
        form = BannerForm(request.POST or None, initial={
        'affiliation': Campaign_Options.new_regent_affiliation}) #added after first answer
    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    print ('fine')
    return render(request, 'core/add_banner.html', {'form': form})
“Affiliazione”字段显示为“红色”,但未保存,因为。“从属关系”字段实际上传递数据,但被隐藏。它们一起给出了我想要的(一个被禁用的字段,它传递数据,这样用户可以看到值,但不能更改它)

问题是我不喜欢硬编码值('red'和'id=1')。我在模型中有“选项”类,我选择要传递的值,但我不知道如何。。。我觉得这是个愚蠢的问题,对不起,有人能帮我吗

My
models.py

class BannerForm(forms.ModelForm):
    name = forms.CharField(max_length=32)
    #Affiliazione = forms.CharField(disabled = True, initial='red') #in original question
    #affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    #widget=forms.HiddenInput(), initial=Affiliation.objects.get(id=1))  #in original question
    Affiliazione = forms.CharField(disabled = True, required=False) #added after first answer
    affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    widget=forms.HiddenInput()) #added after first answer
class Options(models.Model):
    new_affiliation = models.ForeignKey('Affiliation')

class Affiliation(models.Model):
    name = models.CharField(max_length=32, unique=True)
    def __str__(self):
        return self.name

class Banner(models.Model):
    name = models.CharField(max_length=32, unique=True)
    affiliation = models.ForeignKey(Affiliation)
def add_banner(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = BannerForm(request.POST)
        print('form is post') #control
        # Have we been provided with a valid form?
        if form.is_valid():
            print('form is valid') #control
            # Save the new banner to the database.
            banner = form.save(commit=False)
            #some irrilevant code here
            form.save(commit=True)
            print('form salvato') #control
            # Now call the homepage() view.
            # The user will be shown the homepage.
            return homepage(request)
        else:
            # The supplied form contained errors - just print them to the terminal
            print (form.errors)
    else:
        # If the request was not a POST, display the form to enter details
        #form = BannerForm(request.POST) #in original question
        #initial_value = 'red' #added after first answer
        #init = Affiliation.objects.get(id=1) #added after first answer
        form = BannerForm(request.POST or None, initial={
        'affiliation': Campaign_Options.new_regent_affiliation}) #added after first answer
    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    print ('fine')
    return render(request, 'core/add_banner.html', {'form': form})
编辑。我的
视图.py

class BannerForm(forms.ModelForm):
    name = forms.CharField(max_length=32)
    #Affiliazione = forms.CharField(disabled = True, initial='red') #in original question
    #affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    #widget=forms.HiddenInput(), initial=Affiliation.objects.get(id=1))  #in original question
    Affiliazione = forms.CharField(disabled = True, required=False) #added after first answer
    affiliation = forms.ModelChoiceField(Affiliation.objects.all(),
    widget=forms.HiddenInput()) #added after first answer
class Options(models.Model):
    new_affiliation = models.ForeignKey('Affiliation')

class Affiliation(models.Model):
    name = models.CharField(max_length=32, unique=True)
    def __str__(self):
        return self.name

class Banner(models.Model):
    name = models.CharField(max_length=32, unique=True)
    affiliation = models.ForeignKey(Affiliation)
def add_banner(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = BannerForm(request.POST)
        print('form is post') #control
        # Have we been provided with a valid form?
        if form.is_valid():
            print('form is valid') #control
            # Save the new banner to the database.
            banner = form.save(commit=False)
            #some irrilevant code here
            form.save(commit=True)
            print('form salvato') #control
            # Now call the homepage() view.
            # The user will be shown the homepage.
            return homepage(request)
        else:
            # The supplied form contained errors - just print them to the terminal
            print (form.errors)
    else:
        # If the request was not a POST, display the form to enter details
        #form = BannerForm(request.POST) #in original question
        #initial_value = 'red' #added after first answer
        #init = Affiliation.objects.get(id=1) #added after first answer
        form = BannerForm(request.POST or None, initial={
        'affiliation': Campaign_Options.new_regent_affiliation}) #added after first answer
    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    print ('fine')
    return render(request, 'core/add_banner.html', {'form': form})
我的
添加条幅.html

  {% csrf_token %}
  {% for hidden in form.hidden_fields %}
  {{ hidden }}
  {% endfor %}

  {% for field in form.visible_fields %}
    {{ field.errors }}
    {{ field.label }}
    {{ field }}
    {{ field.help_text }}
    <br />
  {% endfor %}
{%csrf\u令牌%}
{%用于隐藏在表单中。隐藏的_字段%}
{{隐藏}}
{%endfor%}
{%form.visible_fields%}
{{field.errors}}
{{field.label}
{{field}}
{{field.help_text}

{%endfor%}
即使我不太了解您表单的意图,但为了回答您的问题,您可以在初始化表单时从视图中传递初始值,这将使该值具有灵活性:

def your_view(request):
    # get the string for affilizione by applying your logic here
    # initial_value = 'red'
    form = BannerForm(request.POST or None, initial={'Affiliazione': initial_value})

我的表单允许用户创建新的横幅(还有其他irrilevant字段)。用户应该看到从属关系值,但不能更改它。你的观点对我的形式没有任何影响。。。场地保持空白,然后你可能还需要调整你的前端。像这样呈现字段:
{{field.label}:{{{field.value}}
只会给出值,而不会呈现小部件。这也不起作用。。。它给出了“Nome:None”和“Affiliazione:None”,但两者都没有文本框。我已经编辑了我的第一篇博文,但是你确定没有更简单的方法在表单中传递初始数据。。。类似于init=Options.new_affiliation,然后Affiliazione=forms.CharField(disabled=True,initial=init)的东西我做过它,现在它工作了,但和以前一样。。。只是我从表单初始化,你从视图初始化。。。问题是一样的:我不喜欢硬编码值('red'和'id=1')。我在模型中有“选项”类,我选择要传递的值,但不知道如何传递。那么,如何将字段“new_affiliation”的值从“Options”类传递到BannerForm或“add_banner”视图中的字段“Affiliazione”和“affiliation”?