2016-10-31 2 views
0

create.htmlに親モデルのインスタンスを選択して、views.pyに送信したいとします。しかし、私はそれを行う方法を理解することはできません。私は次のようにcreate.htmlで製品インスタンスを選択することに成功しましたが、データをviews.pyに送信して保存する方法がわかりません。Django:ModelFormで親モデルのインスタンスを選択

{% for product in products %} 
    <option value="{{ product.id }}">{{ product.name}}</option> 
    <div class="error">{{ form.product.id.errors }}</div> 
{% endfor %} 

create.html

~~~ 
<div> 
    <select> 
    {% for product in products %} 
     <option value="{{ product.id }}">{{ product.name}}</option> 
     <div class="error">{{ form.product.id.errors }}</div> 
    {% endfor %} 
    </select> 
</div> 
<div> 
    <label for="invoice_no">INOVICE NO:</label> 
    {{ form.invoice_no }} 
    <div class="error">{{ form.invoice_no.errors }}</div> 
</div> 
~~~ 

views.py

from django.shortcuts import redirect, render 
from django.http import HttpResponse 
from .models import Invoice 
from account.models import User 
from .forms import InvoiceForm 

def index(request): 
    d = { 
     'invoices' : Invoice.objects.all(), 
    } 
    return render(request, 'invoice/index.html', d) 

def create(request): 
    user = request.user 
    if request.method == 'POST': 
     form = InvoiceForm(request.POST) 
     if form.is_valid(): 
      invoice = form.save(commit=False) 
      invoice.user = user 
      invoice.save() 
      return redirect('/invoice') 
     else: 
      return redirect('/') 
    else: 
     products = user.products.all() 
     form = InvoiceForm() 
     return render(request, 'invoice/create.html', {'form': form, 'products':products}) 

models.py

from django.db import models 

from account.models import User 
from product.models import Product 

class Invoice(models.Model): 
    user = models.ForeignKey(User, verbose_name='User', related_name='invoices', default=1, null=True) 
    product = models.ForeignKey(Product, verbose_name='Product', related_name='invoices', default=1, null=True) 
    invoice_no = models.IntegerField('Invoice No') 

forms.py

from django import forms 
from .models import Invoice 
from product.models import Product 

class InvoiceForm(forms.ModelForm): 
    class Meta: 
     model = Invoice 
     fields = (
      'product', 
      'invoice_no', 
     ) 

     widgets = { 
      'product': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Product'}), 
      'invoice_no': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Invoice No'}), 

     } 

答えて

0

私は解決しました。 name="product"を追加することで、私はそれを達成しました。ご協力ありがとうございます!

<select> 
    {% for product in products %} 
     <option value="{{ product.id }}">{{ product.name}}</option> 
     <div class="error">{{ form.product.id.errors }}</div> 
    {% endfor %} 
    </select> 

    <select name="product"> 
    {% for product in products %} 
     <option value="{{ product.id }}">{{ product.name}}</option> 
     <div class="error">{{ form.product.id.errors }}</div> 
    {% endfor %} 
    </select> 
1

htmlフォームを送信することによって、サーバーにデータを送信します。チュートリアルのステップ4のフォーム例を参照してください - https://docs.djangoproject.com/en/1.10/intro/tutorial04/ 関連情報http://www.w3schools.com/html/html_forms.asp htmlフォームの一般的な説明

+0

あなたの答えに直接話すコードを追加することをお勧めします。 http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers –

+0

ヒントをお寄せいただきありがとうございます。 –

関連する問題