2011-06-27 15 views
7

बनाता है मैं फॉर्म को संपादित कर रहा हूं, यह डेटा को सही ढंग से लोड करता है जब मैं सहेजता हूं तो यह डेटाबेस में नई प्रविष्टि बनाता है।Django में प्रपत्र को संपादित करने से नया उदाहरण

यहाँ दृश्य कार्यों

def create_account(request): 


    if request.method == 'POST': # If the form has been submitted... 
     form = AccountForm(request.POST, request.FILES) # A form bound to the POST data 
     if form.is_valid(): # All validation rules pass 
       form.save() 
       return HttpResponseRedirect('/thanks/') # Redirect after POST 
    else: 
     form = AccountForm() # An unbound form 

    return render_to_response('account_form.html', { 
      'form': form, 
    }) 

है -

def edit_account(request, acc_id): 

    f = Account.objects.get(pk=acc_id) 
    if request.method == 'POST': # If the form has been submitted... 
     form = AccountForm(request.POST, request.FILES) # A form bound to the POST data 
     if form.is_valid(): # All validation rules pass 
       form.save() 
       return HttpResponseRedirect('/thanks/') # Redirect after POST 
    else: 
     form = AccountForm(instance=f) # An unbound form 

    return render_to_response('account_form.html', { 
      'form': form, 
    }) 

मैं सच में संपादन के अलग-अलग समारोह है और हटाने के लिए अलग करने के लिए की जरूरत है। मैं एक समारोह

टेम्पलेट

<form action="/account/" method="post" enctype="multipart/form-data" > 
    {% csrf_token %} 
    {% for field in form %} 
     <div class="fieldWrapper"> 
      {{ field.errors }} 
      {{ field.label_tag }}: {{ field }} 
     </div> 
    {% endfor %} 
    <p><input type="submit" value="Send message" /></p> 
    </form> 
+0

क्या आपका मतलब है कि मुझे बचाने से भिन्न कार्य का उपयोग करना चाहिए ?? – user2134226

+0

कृपया एसएलॉट की टिप्पणी को अनदेखा करें, यह सटीक नहीं है। –

उत्तर

11

आप POST अनुभाग में instance तर्क लापता हैं।

इसके बजाय इस बात का

:

form = AccountForm(request.POST, request.FILES) # A form bound to the POST data 

आप इस का उपयोग करना चाहिए:

form = AccountForm(request.POST, request.FILES, instance=f) # A form bound to the POST data 

एक बार जब आप जोड़ने कि जोड़ें/संपादित फार्म के लिए आप एक ही समय में/संपादन जोड़ने के लिए सक्षम हो जाएगा।

यह instance=None जोड़ देगा और instance एक वास्तविक खाता है तो अपडेट करें।

def edit_account(request, acc_id=None): 
    if acc_id: 
     f = Account.objects.get(pk=acc_id) 
    else: 
     f = None 

    if request.method == 'POST': # If the form has been submitted... 
     form = AccountForm(request.POST, request.FILES, instance=f) # A form bound to the POST data 
     if form.is_valid(): # All validation rules pass 
      form.save() 
      return HttpResponseRedirect('/thanks/') # Redirect after POST 
    else: 
     form = AccountForm(instance=f) # An unbound form 

    return render_to_response('account_form.html', { 
     'form': form, 
    }) 
+0

क्षमा करें दोस्त मैं अभी भी उलझन में हूं। 1) क्या आप उस कोड को प्रदान कर सकते हैं जिसका उपयोग मुझे करना चाहिए जहां संपादन के लिए केवल एक ही फ़ंक्शन है और – user2134226

+0

जोड़ें बनाने में मुझे कोई तर्क नहीं है लेकिन संपादन में मुझे तर्क पारित करने की आवश्यकता है, इसलिए मैं कैसे गठबंधन कर सकता हूं। मुझे becasue संपादित करने के लिए अलग-अलग टेम्पलेट की भी आवश्यकता है, तो मुझे संपादन कार्रवाई को संपादित करने की आवश्यकता है संपादित करें_account – user2134226

+0

@bidu: मैंने एक उदाहरण जोड़ा है, जो काम करना चाहिए :) – Wolph

1

आप कुछ इस तरह की कोशिश की है में सभी कर सकते हैं?

# Create a form to edit an existing Object. 
    a = Account.objects.get(pk=1) 
    f = AccountForm(instance=a) 
    f.save() 

# Create a form to edit an existing Article, but use 
# POST data to populate the form. 
    a = Article.objects.get(pk=1) 
    f = ArticleForm(request.POST, instance=a) 
    f.save() 
संबंधित मुद्दे