2017-04-17 14 views
0

ユーザーからデータを取得しようとしています。私がユーザー情報を表示したいフォームも、私がこの情報を更新するのに使うものと同じです。同じフォームとテンプレートでユーザー情報を作成、取得、編集する

更新3

いくつかの更新の後、私はこの作業を行い、これは私のコードです。 somenoneを行うには良い方法を持っている場合、これはそれを共有することができます:)

models.py

from django.db import models 
from django.contrib.auth.models import User 
# Create your models here. 
class informacionFacturacion(models.Model): 
    usuario = models.ForeignKey(User, on_delete=models.CASCADE) 
    apellidos = models.CharField(max_length=100) 
    nombres = models.CharField(max_length=100) 
    [More fields...] 
    def __str__(self): 
     self.apellidos 

forms.py

from .models import informacionFacturacion 
#Create your forms here. 
class informacionFacturacionForm(ModelForm): 
    class Meta: 
     model = informacionFacturacion 
     fields = [ 
      "usuario", 
      "apellidos", 
      "nombres", 
      [More fields...] 
     ] 

views.py

@login_required 
def datosPersonales(request): 
    #Filter query by user ID 
    query = informacionFacturacion.objects.filter(usuario=request.user) 

    form = informacionFacturacionForm() 
    #If query has content, edit record, else, create a new record 
    if query: 
     if request.method == "POST": 
      form = informacionFacturacionForm(request.POST or None, instance=query[0]) 
      if form.is_valid(): 
       edit_content = form.save() 
       edit_content.save() 

    else: 
     if request.method == "POST": 
      form = informacionFacturacionForm(request.POST) 
      if form.is_valid(): 
       create_content = form.save(commit=False) 
       create_content.save() 
       return HttpResponseRedirect(reverse('datosPersonales')) 

    context = { 
     "titulo": "Datos personales | Co.", 
     "body_class": "class= sidebar_main_open sidebar_main_swipe", 
     "form": form, 
     "infoFacturacion": query, 
    } 
    template = "micuenta/datosPersonales.html" 
    return render(request, template, context) 

ありがとうございます。

答えて

1

一見して、informacionFacturacionテーブルにデータが入力されていないようです。 instance.save()に達していることを確認しましたか? (言い換えれば、フォームが有効である)

第2に、テンプレートでinformacionFacturacionオブジェクトをフォーム要素として使用したいと考えており、それらを個別に処理しています。

if request.POST: 
    form = informacionFacturacionForm(request.POST) 
    if form.is_valid(): 
     instance = form.save(commit=False) 
     instance.save() 
    else: 
     # handle here the form error's, maybe report it in the template 
else: 
    query = informacionFacturacion.objects.filter(usuario=request.user) 
    form = informacionFacturacionForm(instance=query[0]) 

をしてinfoFacturacionformパラメータINSEADをレンダリング:やる

{{ form.as_p }} 

を最後に、あなたのテンプレートのフォームIDのは、そうでない場合は、フォームが満たされることはありません、フォーム要素の名前と一致していることを確認してください。あなたの編集に基づいて

UPDATE

を、今エラーがこの行である:

form = informacionFacturacionForm(request.POST, instance=query_id) 

query_idintであり、それはモデルを期待しています。コードは多くのことを簡素化することができるが、今のために働く必要があります

form = informacionFacturacionForm(request.POST, instance=query) 

query = informacionFacturacion.objects.get(usuario=request.user) 

とに障害のあるラインに

query_id = informacionFacturacion.objects.get(usuario=request.user).id 

:次の行を変更します。ここで

EDIT 2

は、私はあなたが欲しいと仮定するものである:

@login_required 
def datosPersonales(request): 
    query = informacionFacturacion.objects.filter(usuario=request.user) 

    if request.method == "POST": # This will handle the template form's POST 
     form = informacionFacturacionForm(request.POST) 
     if form.is_valid(): 
      asd = form.save(commit=False) 
      asd.save() 
      # Here you may want to redirect to somewhere else 
    # Im not sure here, I guess that you want to handle the GET method if 
    # there is no form in the request. Post your template form to see what 
    # is happening. 
    else: 
     form = informacionFacturacionForm(instance=query) 
     # you dont need to save it, it is already in DB 

    context = { 
     "titulo": "Datos personales | Co.", 
     "body_class": "class= sidebar_main_open sidebar_main_swipe", 
     # I think here is your main issue, you are handling a form object 
     # AND a infoFacturacion object. You need to use just the 
     # form object in the template and render it accordingly. 
     "form": form, 
     "infoFacturacion": query, 
    } 
    template = "micuenta/datosPersonales.html" 
    return render(request, template, context) 
+0

save()を使用してコンテンツを編集できないことがわかりました。私はテーブルが内容を持っているかどうかをチェックするif文を書くでしょう。私はそれを行うことができます:if infoFacturacion:#show edit form else:#show create form。右? –

+0

「コンテンツを編集する」とはどういう意味ですか?オブジェクトのフォームとテンプレート内のフォームは、両方とも変更可能です。テンプレートから来るものを見たい場合は 'print request.POST'を使うことができます。ビューのPOSTメソッドで送られたフォームの要素を含む辞書を表示します。 –

+0

私がここでやろうとしているのは、テーブルコンテンツを同じビューに表示し、どこで編集できるかを示すことです。私は今いくつかのコードを試し、私はポストを更新します。 –

0

まあ、私は私のsytem上の同じ問題とあったので、私はこのソリューションを作った、多分それはあなたに動作します!= D

私は送信ボタンの値を変更し、同じ形式使用しています:

<button type="submit" id="submitButton" name="button" value="">Save</button> 

新しいタスクがある場合は、私はjQueryを使って、ボタンの値を変更します。

$('#submitButton').val('new'); 

とエディションがある場合、私は再び値を変更します。私のviews.pyに

$('#submitButton').val('edit'); 

、私が編集したりすることにより、保存、新規であるかどうかを確認ボタンの値:

def index(request): 
tasks = Task.object.filter() 
context = { 
    'tasks': tasks 
} 
if request.method == 'POST': 
    form = NewTask(request.POST or None) 
    if request.POST['button'] == 'new': 
     if form.is_valid(): 
      context['is_valid'] = True 
      form.save() 
      form = NewTask() 
     else: 
      context['is_valid'] = False 

    if request.POST['button'] == 'edit': 
     instance = Task.object.filter(pk=request.POST['id']).first() 
     form = NewTask(request.POST, instance=instance) 
     if form.is_valid(): 
      context['is_valid'] = True 
      form.save() 
     else: 
      context['is_valid'] = False 
else: 
    form = NewTask() 

context['form'] = form 
return render(request, 'index.html', context) 
関連する問題