python - Getting Previous URL in Django Form -
so i'm trying build something, users able report on site. here's model,
class report(models.model): reporting_url = models.urlfield() message = models.textfield()
here's view,
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 can't use like,
url_report = request.get_full_path()
since need create/edit several views & repeat things in case.
when i'm using like,
url_report = request.meta.get('http_referer')
it's returning url of same page written. i'm using like,
<a href="{% url 'contact:report' %}">report</a>
to reach report form several different apps/html_pages. how can url of previous page user has pressed "report" button?
please me code!
you store referer in session whenever report
button pressed:
def report(request): if request.method == 'get': request.session['report_url'] = request.meta.get('http_referer') # ... if request.method == 'post': form = reportform(request.post or none) if form.is_valid(): new_form = form.save(commit=false) new_form.reporting_url = request.session.get('report_url') new_form.save()
you have persist referer beyond 1 request-response cycle. session designated way that. option render url hidden form field, can tampered with.
Comments
Post a Comment