Home > Software engineering >  How to do model instance in django update views
How to do model instance in django update views

Time:03-07

views.py

def visitor(request):
    fruser = FRUser.objects.get(id=1)
    if request.method == "POST":
        fruser.uid = request.POST.get('visitor_nric_no')
        fruser.name = request.POST.get('name')
        fruser.company_name = request.POST.get('company_name')
        fruser.user_type = request.POST.get('userType')
        fruser.visit_purpose = request.POST.get('purposeOfVisit')
        fruser.valid_from_date = request.POST.get('validFrom')
        fruser.valid_till_date = request.POST.get('validTill')
        fruser.save()
        print(fruser.name)
        return render(request,'kiosk/visitor-checkIn/photo-registration.html',{'fruser':fruser})
    else:
        return render(request, 'kiosk/visitor-checkIn/visitor-new-registration.html')

models.py

class FRUser(models.Model):
    image = models.ForeignKey(to=Image, on_delete=models.CASCADE, null=True)
    frtemplate = models.ForeignKey(to=FRTemplate, on_delete=models.CASCADE, null=True)
    name = models.TextField(null=True)
    user_type = models.TextField(null=True)
    uid = models.TextField(null=True)
    company_name = models.TextField(blank=True, null=True)
    visit_purpose = models.TextField(blank=True, null=True)
    employee_number = models.TextField(blank=True, null=True)
    designation = models.TextField(blank=True, null=True)
    valid_from_date = models.DateField(null=True)
    valid_till_date = models.DateField(null=True)
    valid_from_time = models.TimeField(null=True)
    valid_till_time = models.TimeField(null=True)
    is_blacklisted = models.BooleanField(default=False, null=True)    
    is_other_visit_purpose = models.BooleanField(default=False, null=True)    
    is_active = models.BooleanField(default=True, null=True)
    created_date = models.DateTimeField(auto_now_add=True, null=True)
    created_by = models.ForeignKey(to=User, on_delete=models.CASCADE, related_name=' ', null=True)
    modified_date = models.DateTimeField(auto_now=True, null=True)
    modified_by = models.ForeignKey(to=User, on_delete=models.CASCADE, related_name=' ', null=True)

html

  <form id="visitorRegistration" data-bind="submit: save"  method="post" action="/kiosk/visitor/{{fruser.id}}"> 
              <div >
                <div >
                  <div >
                    <label >User Type</label>
                    <input type="text"  value="{{ fruser.user_type }}" disabled="disabled" name="userType">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Name <i >*</i></label>
                    <input type="text"  name="name" data-bind="value: name" value="{{ fruser.name }}" >
                  </div>
                </div>
                <div >
                  <div >
                    <label >NRIC/FIN Number</label>
                    <input type="text"   name="visitor_nric_no" value="{{ fruser.uid }}">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Purpose of visit <i >*</i></label>
                    <select  name="purposeOfVisit" value="{{ fruser.visit_purpose }}">
                    </select>
                  </div>
                </div>
                <div >
                  <div >
                    <label >Company Name <i >*</i></label>
                    <input type="text"  name="company_name" data-bind="value: company_name" value="{{ fruser.company_name }}">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Mobile Number <i >*</i></label>
                    <input type="text"  name="mobileNumber">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Access Template</label>
                    <input type="text"  disabled="disabled" name="accessTemplate">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Valid From</label>
                    <input type="text"  disabled="disabled" name="validFrom">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Valid Till</label>
                    <input type="text"  disabled="disabled" name="validTill">
                  </div>
                </div>
                <div >
                  <div >
                    <label >Facility <i >*</i></label>
                    <select  name="facility"></select>
                  </div>
                </div>
                <div >
                  <div >
                    <label >Block <i >*</i></label>
                    <select  multiple="multiple" name="block">
                      <option>Block1</option>
                      <option>Block2</option>
                    </select>
                  </div>
                </div>
                <div >
                  <div >
                    <label >&nbsp;</label>
                    <button type="button" ><i ></i></button>
                  </div>
                </div>

                <div >
                  <button type="button" >Back</button>
                  <button type="submit" data-bind="click: save" >Next</button>
                </div>
              </div>
            </form>

I want to do update view of this html page How to get instance of FRUser model I did'nt get how to do this I need FRUser model instance data in this html page I need instance name,company_name like that all i need in html page how to do this When i given id=1, it also not displaying id = 1 data please help me thanks in advance

CodePudding user response:

In your html, do:

{% if fruser %}
    {{fruser.name}}
{% endif %}

This should print out the name on the html.

CodePudding user response:

I would use a Django Form as it would do this automatically for you

But to fix this specific problem, you need to pass fruser to your template kiosk/visitor-checkIn/visitor-new-registration.html

You can do this the same way you did for your template kiosk/visitor-checkIn/photo-registration.html

return render(request, 'kiosk/visitor-checkIn/visitor-new-registration.html', {'fruser':fruser})
  • Related