Django / Jquery / Javascript - How to fill out a form with url parameters (form autocomplete)

I want to send a link for some people:

http://www.example.com/form/ ?q=example@me.com

If they click on the link, it will go to the page / form / where is the form with several fields.

I would like to pre-populate it with this q line, which would fill this email input

with the specified email address in the string " example@me.com ".

The website is made in Django, I would like to ask what is the best approach to achieve this.

Should I use JS, jQuery, Django? I don’t know where to start.

I would like something like this:

http://support.unbounce.com/entries/307658-Can-I-pre-populate-a-form-with-URL-parameters-VIDEO-

but on my site. Can someone help with a simple form and example?

thanks

models.py

from django.db import models class Example(models.Model): username = models.CharField(max_length=200, blank=True) email = models.CharField(max_length=200) image = models.ImageField(upload_to='profiles', blank=True) text = models.CharField(max_length=200, blank=True) 

forms.py

 from django import forms from models import Example class ExampleForm(forms.ModelForm): class Meta: model = Example 

views.py

 from django import forms from models import Example from forms import ExampleForm from django.template import RequestContext, Context from django.core.context_processors import csrf from django.shortcuts import render_to_response from django.shortcuts import render_to_response, get_object_or_404 from django import http import os import json from django.http import HttpResponse from django.http import HttpResponseRedirect from django.conf import settings def index(request): if request.method == "POST": form = ExampleForm(request.POST, request.FILES) if form.is_valid(): form.save() return http.HttpResponseRedirect('/success/') else: form = ExampleForm() return render_to_response('index.html', {'title': 'Example Form', 'form': form},context_instance=RequestContext(request)) 

urls.py

 from django.conf.urls import patterns, include, url from django.conf import settings from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^$', 'signup.views.index', name='index'), url(r'^admin/', include(admin.site.urls)), ) 

index pattern

 <form id='add_cat' method='post' enctype="multipart/form-data">{% csrf_token %} <input id="id_username" maxlength="200" name="username" type="text" placeholder="username"> <input id="id_email" maxlength="200" name="email" type="text" > <input id="id_picture" maxlength="200" name="picture" type="text"> <textarea id="id_text" row="3" name="first_text" placeholder="Type something…"></textarea> <input id="add_cat_btn" type='submit' value="save"> </form> 
+4
source share
3 answers

request.GET.copy() will provide you with a dictionary and pass it as an initial dictionary.

 initial = request.GET.copy() form = MyForm(initial=initial) 

It will fill in the fields that are present on the form.

If you had GET and POST interchangeably,

 def index(request): initial = {} if request.GET: initial = request.GET.copy() form = ExampleForm(initial=initial) if request.method == "POST": if request.POST: initial = request.POST.copy() form = ExampleForm(request.POST, request.FILES, initial=initial) if form.is_valid(): form.save() return http.HttpResponseRedirect('/success/') return render_to_response('index.html', {'title': 'Example Form', 'form': form },context_instance=RequestContext(request)) 
+3
source

You can do this in either Django or JavaScript.

Here is a JavaScript solution using getParameterByName from this SO question.

The advantage of this in JS is that it allows you to cache the server response and not require server calculations for every user request.

Enter the email form ID field, for example emailfield , then you can do something like

 var emailField = document.getElementById("emailfield"); emailField.value = getParameterByName("email"); // "q" in your example url 

Inside your ready or onload handler (or just in a script tag placed like the last in the body section)

Here is the fiddle

This does not work, as it puts the code in an iframe, so you cannot access the GET parameter, if you check the iframe, you will see that it works:

Here is an example http://fiddle.jshell.net/FA9fQ/show/light/?email=hello

+2
source

karthikr's answer is correct. For those who are looking for a way to do this using the Django Class, this can be done using the get_initial method. The following is an example of use:

 class RequestInitialFormViewMixin(object): """Pass Request.GET as initial values to Form""" def get_initial(self): initial = super(RequestInitialFormViewMixin, self).get_initial() if self.request.GET: for key, value in self.request.GET.items(): # QueryDict, each value is a list if len(value) == 1: initial[key] = value[0] else: initial[key] = value return initial class MyFormView(RequestInitialFormViewMixin, FormView): # ... 

Note that request.GET returns a QueryDict , which has a list for each value.

0
source

Source: https://habr.com/ru/post/1483197/


All Articles