0

This is my models.py.

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

class User_data(models.Model):

    user_ID = models.CharField(max_length = 20)
    name = models.CharField(max_length = 50)
    user = models.ForeignKey(User, on_delete = models.CASCADE, null =True)

    def __str__(self):
        return self.name

This is my forms.py

from django import forms
from lrequests.models import User_data

class UserForm(forms.ModelForm): 
    class Meta:
        fields = ("name", "user_ID")

        model = User_data

This is my views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import UserForm
from django.shortcuts import render_to_response
from .models import User_data

def get(request):
    form_class = UserForm
    if request.method == "POST":
        form = UserForm(request.POST)
        if form.is_valid():
            data = form.save(commit = False)
            data.user = request.user
            form.save()
        return HttpResponse("Sucessfully submitted")
    else:
        form = UserForm()
        return render(request, "request_form.html", {'form' : form_class})

#update
def auto_fill_form(request):
form = UserForm(initial = dict(name = request.user.first_name))
context = dict(form=form)
return render(request, "request_form.html", context)

Now, I've tried populating the user data, which was specified whilst creating account. So, that he(user) doesn't tamper the data (as it need to read-only) and it is automatically filled without the user giving the input. I've read django documentation but it specifies only dynamically initialising data see here. I've tried putting that code in the forms.py, but it didn't work.

I've even tried in the HTML template, that didn't work too.

            {% csrf_token %}
            <div class="form-row">
                {{ form.name.errors }}
                {{ form.name.label_tag }} {{ form.name = user.first_name }}
            </div>
            {% comment %} {{ form.as_p }} {% endcomment %}

How do I get data by default into the form? Can someone help me on this?

bhanuprakash
  • 149
  • 3
  • 16
  • this will help https://stackoverflow.com/questions/47076529/initialize-form-with-request-user-in-a-modelform-django – Mohit Solanki Aug 23 '18 at 06:05
  • @MohitSolanki negative! – bhanuprakash Aug 23 '18 at 06:28
  • What is `{'form' : form_class}`? It should be `{'form' : form}`. – hygull Aug 23 '18 at 06:50
  • @hygull `form_class = UserForm` in views.py – bhanuprakash Aug 23 '18 at 06:51
  • Visiting a page makes GET request to server so else part will be executed where you're creating `form` but you're not using it, why? Please try and let us know. Also have a look at https://docs.djangoproject.com/en/2.1/topics/forms/. This is for Django 2.1. Based on your Django version you can switch to it by just changing the version in address bar to your. – hygull Aug 23 '18 at 06:56

1 Answers1

1

Just replace

else:
    form = UserForm()
    return render(request, "request_form.html", {'form' : form_class})

by:

else:
    return auto_fill_form(request)
albar
  • 3,020
  • 1
  • 14
  • 27