Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django form not showing up in template using {{ form }} tag

I have made a basic todo app for my practice in which i want to create a profile page for every user.
I have made a profile model, and connect it to a User signal, profile has been created but when I render it in a template, its not showing the profile form.

the form for the profile model is form.py:

class ProfileForm(ModelForm):
    class Meta:
        model = Profile
        fields = '__all__'
        exclude = ['user']

model.py –profile model:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, null=True, blank=False)
    email = models.EmailField(max_length=200, null=True)
    phone = models.CharField(max_length=40, null=True, blank=True)
    image = models.ImageField(null=True, blank=True, default='pfp.png', upload_to='images')
    date_created = models.DateField(auto_now_add=True)
    
    def __str__(self):
        return self.user.username

the view to render the profile(note: I am not creating a edit profile form, which i’ll do later, that is why i used get in method):

def user_profile(request):
    profile = request.user.profile
    form = ProfileForm(instance=profile)
    
    context = {'form': form}
    return render(request, 'task/profile.html')

profile page template:

{% extends 'main.html' %}

{% load crispy_forms_tags %}
{% block content %}

<h3>Profile Page</h3>

<style>
    .profile-pic{
        max-width: 200px;
        max-height: 200px;
        margin: 0 auto;
        border-radius: 50%;
    }
    body{
        background-color: rgba(211, 211, 211, 0.527);
    }
</style>

<br>
<div class="row">
    <div class="col-md-3">
        <div class="card card-body">
            <a class="btn btn-warning" href="{% url 'home' %}">Back to home page</a>
            <hr>
            <h3 style="text-align: center">Account Settings</h3>
            <hr>
            <img src="{{ user.profile.image.url }}" alt="" class="profile-pic">
        </div>
    </div>
<form action="" method="get" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form|crispy }}

    <input type="submit" class="btn btn-primary" name=" Update Information">
</form>

{% endblock content %}

what am I doing wrong here

>Solution :

You need to pass the context to the render engine:

def user_profile(request):
    profile = request.user.profile
    form = ProfileForm(instance=profile)
    
    context = {'form': form}
    #                              add the context 🖟
    return render(request, 'task/profile.html', context)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading