2016-07-12 17 views
0

私はdjangoの新機能で、HTMLファイルにフォームを表示しようとしています。ブラウザのこの特定のページにアクセスするとフィールドが表示されません。誰もがなぜアイデアを持っていますか?私は add_device.htmlDjangoのフォームフィールドが表示されない

{% extends 'layout/layout1.html' %} 
{% block content %} 
    <form action = "userprofile/" method = "post"> 
     {% csrf_token %} 
     {{ form }} 
     <input type = "submit" value = "Submit"/> 
    </form> 
{% endblock %} 

forms.py

from django import forms 
from models import UserProfile 

class UserProfileForm(forms.ModelForm): 
    class Meta: 
     model = UserProfile 
     fields = ('deviceNb',) 

models.py

from django.db import models 
from django.contrib.auth.models import User 


class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    deviceNb = models.CharField(max_length = 100) 

User.profile = property(lambda u : UserProfile.objects.get_or_create(user = u)[0]) 
を示すフォーム以外のすべてを見ることができている:ここでは

は、htmlファイルであります

views.py

def user_profile(request): 
    if request.method == 'POST': 
     #we want to populate the form with the original instance of the profile model and insert POST info on top of it 
     form = UserProfileForm(request.POST, instance=request.user.profile) 

     if form.is_valid: 
      form.save() 
      #to go back to check that the info has changed 
      return HttpResponseRedirect('/accounts/loggedin') 

     else: 
      #this is the preferred way to get a users info, it is stored that way 
      user = request.user 
      profile = user.profile 
      #if we have a user that has already selected info, it will pass in this info 
      form = UserProfileForm(instance=profile) 

     args = {} 
     args.update(csrf(request)) 
     args['form'] = form 

     print(form) 

     return render_to_response('profile.html',args) 

私は正しいURLにアクセスするので、私のURLファイルは大丈夫だと私は確信しています。私の問題は実際には表示されないフォームフィールドです。

ありがとうございます!

答えて

2

あなたの意見でGETリクエストを処理していません。このサンプルコードである

def user_profile(request): 
    if request.method == 'POST': 
    # your existing code 
    # ..... 
    else : #when its get request 
     form = UserProfileForm(instance=request.user.profile) 
     args = {} 
     args.update(csrf(request)) 
     args['form'] = form 

     return render_to_response('profile.html',args) 

としてビューのコードを更新し、それを向上させることができます。

+0

おかげで、それが問題だったまさにです。気づいて、指摘していただきありがとうございます:)それは今動作します! – Rose

1

ビューのインデントが正しくありません。 elseブロックはif request.method == 'POST'ステートメントに属し、GET要求を処理します。

また、getおよびpost要求に対する応答を返すように、メソッドの最後にインデントを修正する必要があります。廃止されたrender_to_responseの代わりにrenderを使用する方が良いでしょう。 args.update(csrf(request))を呼び出す必要がなくなるため、コードが簡単になります。

from django.shortcuts import render 

def user_profile(request): 
    if request.method == 'POST': 
     #we want to populate the form with the original instance of the profile model and insert POST info on top of it 
     form = UserProfileForm(request.POST, instance=request.user.profile) 

     if form.is_valid: 
      form.save() 
      #to go back to check that the info has changed 
      return HttpResponseRedirect('/accounts/loggedin') 

    else: 
     #this is the preferred way to get a users info, it is stored that way 
     user = request.user 
     profile = user.profile 
     #if we have a user that has already selected info, it will pass in this info 
     form = UserProfileForm(instance=profile) 

    args = {} 
    args['form'] = form 

    return render(request, 'profile.html', args) 
+1

ありがとう!私がそこでやったそのような間違いです:)それを指摘し、それの上にヒントを与えていただきありがとうございます:)私は非常に感謝します。それは今働きます! – Rose

1

GETリクエストも処理する必要があります。あなたのビューでこれを試してみてください:

def user_profile(request): 
    form = UserProfileForm() 
    if request.method == 'GET': 
     # handle GET request here 
     form = UserProfileForm(instance=request.user.profile) 

    elif request.method == 'POST': 
     #we want to populate the form with the original instance of the profile model and insert POST info on top of it 
     form = UserProfileForm(request.POST, instance=request.user.profile) 
     if form.is_valid: 
      form.save() 
      #to go back to check that the info has changed 
      return HttpResponseRedirect('/accounts/loggedin') 
    args = {} 
    args['form'] = form 
    return render_to_response('profile.html',args) 

そして、あなたのprofile.htmlに、あなたはこのような何か行うことができます。

{{ form.as_p }} 
+0

ありがとうアリックス:)確かに、私は正しくGETを扱っていなかった、それは今、ありがとう! – Rose

+0

ようこそ。あなたが助けてくれたら質問に投票してください:) @Rose – alix

関連する問題