Django - Modelform not rendering

时间:2017-12-18 07:27:12

标签: django forms views modelform

I created a form to update a User's profile, however when I run it, there are no errors, but when I try to open up the page, the header appears but the UpdateBioForm does not appear. Secondly, I was wondering how you would create a large textbox to store someone's biography.

Models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    biography = models.CharField(max_length = 255, default = '')
    city = models.CharField(max_length=100, default = '')
    website = models.URLField(default='')
    image = models.ImageField(upload_to='profile_image', blank=True)

    def setdefault(self, default_path='/profile_image/Default.jpg'):
        if self.image:
            return self.image
        return default_path

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

Forms.Py

class UpdateBioForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        fields = (
            'biography',
            'city',
            'website'
        )

    def save(self, commit=True):
        savedBio = super(UpdateBioForm, self).save(commit=False)
        savedBio.biography = self.cleaned_data['biography']
        savedBio.city = self.cleaned_data['city']
        savedBio.website = self.cleaned_data['website']

        if commit:
            savedBio.save()

        return savedBio

Views.py

def update_bio(request):
    if request.method == 'POST':
        form = UpdateBioForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect('/')
    else:
        form = UpdateBioForm(instance=request.user)
    args = {'form':form}
    return render(request, 'accounts/update_bio.html')

urls.py

url(r'^profile/updatebio/$',views.update_bio, name='update_bio'),

update_bio.html

{% extends 'base.html' %}

{% block body %}
<div class="container">
    <h1>Update Biography</h1>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
</div>
{% endblock %}

1 个答案:

答案 0 :(得分:1)

You are not passing any context to your SELECT DISTINCT t1.mobile FROM table_1 t1 INNER JOIN table_1 t2 ON t1.name = t2.name AND t1.age = t2.age method - you define render() but don't do anything with that variable. Change it to:

args