表格没有在html中显示 - django

时间:2015-05-19 20:19:54

标签: python django django-models django-forms modelform

我有一个模型扩展了django中的用户模型,该模型是用户配置文件,我将用于将附加信息与用户模型相关联。我有模型,表单,视图,网址和html,但html没有显示fiels,只显示提交按钮。

这是代码:

models.py

class userProfile(models.Model):

    def url(self, filename):
        ruta = "MultimediaData/Users/%s/%s"%(self.user.username, filename)
        return ruta

    user    =   models.ForeignKey(User, unique=True)
    photo    =   models.ImageField(upload_to= url)
    telefono=   models.CharField(max_length=30)

    def __srt__(self):
        return self.user.username

forms.py

class userProfileForm(forms.Form):
    class Meta:
        model = userProfile

views.py

def perfil_view(request):
    if request.method == 'POST':
         form = userProfileForm(request.POST)
         if form.is_valid():
            user = request
            perfil = userProfileForm(user = form.cleaned_data.get('user'),
                                    photo = form.cleaned_data['photo'],
                                    telefono = form.cleaned_data['telefono'],
                                    )
            perfil.save()
            ctx ={'form':form}
            return render_to_response('prueba.html',ctx,context_instance=RequestContext(request))
         else:
            ctx ={'form':form}
            return render_to_response('perfil.html',ctx,context_instance=RequestContext(request))
    else:
         form = userProfileForm()
    ctx ={'form':form}
    return render_to_response('prueba.html',ctx,context_instance=RequestContext(request))

prueba.html

{% extends "base2.html" %}
{% block title %}Prueba{% endblock %}
{% block content %}
    <form action="." method="post">
            {% csrf_token %}
        {{ form.as_p }}
    <input type="submit" value="Submit">
    </form>
{% endblock %}

settings.py

AUTH_PROFILE_MODULE = 'melomanos.userProfile'

1 个答案:

答案 0 :(得分:0)

尝试使用:

return render(request, 'prueba.html', ctx)

而不是

return render_to_response('prueba.html',ctx,context_instance=RequestContext(request))

另外,请不要按如下方式导入渲染:

from django.shortcuts import render