1

So i'm trying to build something, so that users would be able to report something on site. Here's the model,

class Report(models.Model):
    reporting_url = models.URLField()
    message = models.TextField()

I created Form for this Model including 'message' field only because 'reporting_url' is something it needs to populate by itself depending upon the specific page from where user has clicked "Report" button.

def report(request):
url_report = ???
if request.method == 'POST':
    form = ReportForm(request.POST or None)
    if form.is_valid():
        new_form = form.save(commit=False)
        new_form.reporting_url = url_report
        new_form.save()

I was wondering How can I pass the specific url to 'reporting_url' field in form depending on the Page from where user has clicked "Report" button? (Much like s we see on social Networks).

Am I doing this correctly, Or is there a better way for doing this? Please help me with this code. Thanks in Advance!

2 Answers2

1

If there is a report button on that specific page then I believe you could write custom context processor.

More info: Django: get URL of current page, including parameters, in a template

https://docs.djangoproject.com/en/1.11/ref/templates/api/

Or maybe just write it directly in the views.py in your function and set

url_report = request.get_full_path()
jamiroquai93
  • 27
  • 1
  • 6
0

I think you can use the form on the same page of the URL and use:

 url_report = request.get_full_path()

in the view, to get the current URL.

Else if you want to create a separate view for the reporting form. You can use

 url_report = request.META.get('HTTP_REFERER')

to get the previous or refering URL which led the user to that page.

request.META.get('HTTP_REFERER') will return None if it come from a different website.

Ajmal Noushad
  • 926
  • 7
  • 18
  • Sir, url_report = request.META.get('HTTP_REFERER') is not returning previous or refering URL. Instead it's returning the same page url from which form is being written –  Sep 12 '17 at 05:02
  • request.META.get('HTTP_REFERER') will return the same page url, if it was accessed from the same page. That is like clicking the link for the page from the same page, otherwise it returns the referring url. Try it with other views and confirm. – Ajmal Noushad Sep 12 '17 at 06:43