Connect the username when sending a report in django

  django, django-forms, django-models, django-views, python

I am beginner to django and I am trying to create a site to send reports after creating a profile, but when I send the report, the username of the author of the report is not saved, Just (None). I searched a lot about it and did not find it

models.py

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


class Profile(models.Model):
    user = models.OneToOneField(User, verbose_name=("user"), null=True, on_delete=models.CASCADE)
    identity_No = models.IntegerField( blank=True, null=True)
    mobile_No = models.IntegerField( blank=True, null=True)
    city = models.CharField( max_length=15,null=True)



class Meta:
    verbose_name =("Profile")
    verbose_name_plural =("Profile")

def __str__(self):
    return '%s' %(self.user)




class Report(models.Model):
    user = models.ForeignKey(User, verbose_name=("user"), on_delete=models.CASCADE)
    Report_type =models.CharField( max_length=100, blank=True, null=True)
    Region =models.CharField( max_length=30, blank=True, null=True)
    city =models.CharField( max_length=30, blank=True, null=True)
    sector =models.CharField( max_length=30, blank=True, null=True)
    report =models.TextField( max_length=3000, blank=True, null=True)
    files =models.FileField( blank=True, null=True)


    class Meta:
        verbose_name =("Report")
        verbose_name_plural =("Report")


    def __str__(self):
        return '%s' %(self.user)

form.py
Shorten the time, attach only the report

class ReportForm(forms.ModelForm):


    class Meta:
        model = Report
        fields = ['Report_type', 'Region', 'city','sector','report','files' ]

view.py

def report(requst):
    if requst.method == 'POST':
        report_form = ReportForm(requst.POST)
        if report_form.is_valid() :
            report_form.save()
            return redirect ('accounts:home')
    else:
        report_form = ReportForm()
    return render(requst, 'user/report.html', {
        'report_form': report_form,

enter image description here })

Source: Python Questions

LEAVE A COMMENT