Passing an initial value into a form field from previous detail view












0















I a bit confused how to do this, I have a "contact user" button on a user detail page. I am trying to set the initial form value for the email field as the users email from the previous detail page view, so that it pre-populates.



Would I pass the user.email as a kwarg into the button url?



views.py



def Contact(request):
form_class = ContactForm

# new logic!
if request.method == 'POST':

form = form_class(data=request.POST)

if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
form_content = request.POST.get('content', '')

# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
}
content = template.render(context)

email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['youremail@gmail.com'],
headers = {'Reply-To': contact_email }
)
email.send()
return redirect('contact_form')


return render(request, 'portal/contact_form.html', {
'form': form_class,
})


forms.py



from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True, initial='{ kwarg }')
content = forms.CharField(
required=True,
widget=forms.Textarea
)


user_detail.html



<div class="col-lg-4 text-center p-5" style="padding-right:20px; border-right: 1px solid #e5e5e5;">
<a href="{% url 'portal:contact_form' kwarg.user.email %}">
<h1><i class="fa fa-2x fa-envelope-o text-success" aria-hidden="true"></i></h1>
<p class="text-muted mt-3 ">Contact Customer</p>
</a>
</div>









share|improve this question























  • Why not use request.session ? or you wanna look into how to pass variables with kwargs

    – nara_l
    Nov 22 '18 at 1:32
















0















I a bit confused how to do this, I have a "contact user" button on a user detail page. I am trying to set the initial form value for the email field as the users email from the previous detail page view, so that it pre-populates.



Would I pass the user.email as a kwarg into the button url?



views.py



def Contact(request):
form_class = ContactForm

# new logic!
if request.method == 'POST':

form = form_class(data=request.POST)

if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
form_content = request.POST.get('content', '')

# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
}
content = template.render(context)

email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['youremail@gmail.com'],
headers = {'Reply-To': contact_email }
)
email.send()
return redirect('contact_form')


return render(request, 'portal/contact_form.html', {
'form': form_class,
})


forms.py



from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True, initial='{ kwarg }')
content = forms.CharField(
required=True,
widget=forms.Textarea
)


user_detail.html



<div class="col-lg-4 text-center p-5" style="padding-right:20px; border-right: 1px solid #e5e5e5;">
<a href="{% url 'portal:contact_form' kwarg.user.email %}">
<h1><i class="fa fa-2x fa-envelope-o text-success" aria-hidden="true"></i></h1>
<p class="text-muted mt-3 ">Contact Customer</p>
</a>
</div>









share|improve this question























  • Why not use request.session ? or you wanna look into how to pass variables with kwargs

    – nara_l
    Nov 22 '18 at 1:32














0












0








0








I a bit confused how to do this, I have a "contact user" button on a user detail page. I am trying to set the initial form value for the email field as the users email from the previous detail page view, so that it pre-populates.



Would I pass the user.email as a kwarg into the button url?



views.py



def Contact(request):
form_class = ContactForm

# new logic!
if request.method == 'POST':

form = form_class(data=request.POST)

if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
form_content = request.POST.get('content', '')

# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
}
content = template.render(context)

email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['youremail@gmail.com'],
headers = {'Reply-To': contact_email }
)
email.send()
return redirect('contact_form')


return render(request, 'portal/contact_form.html', {
'form': form_class,
})


forms.py



from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True, initial='{ kwarg }')
content = forms.CharField(
required=True,
widget=forms.Textarea
)


user_detail.html



<div class="col-lg-4 text-center p-5" style="padding-right:20px; border-right: 1px solid #e5e5e5;">
<a href="{% url 'portal:contact_form' kwarg.user.email %}">
<h1><i class="fa fa-2x fa-envelope-o text-success" aria-hidden="true"></i></h1>
<p class="text-muted mt-3 ">Contact Customer</p>
</a>
</div>









share|improve this question














I a bit confused how to do this, I have a "contact user" button on a user detail page. I am trying to set the initial form value for the email field as the users email from the previous detail page view, so that it pre-populates.



Would I pass the user.email as a kwarg into the button url?



views.py



def Contact(request):
form_class = ContactForm

# new logic!
if request.method == 'POST':

form = form_class(data=request.POST)

if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
form_content = request.POST.get('content', '')

# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
}
content = template.render(context)

email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['youremail@gmail.com'],
headers = {'Reply-To': contact_email }
)
email.send()
return redirect('contact_form')


return render(request, 'portal/contact_form.html', {
'form': form_class,
})


forms.py



from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True, initial='{ kwarg }')
content = forms.CharField(
required=True,
widget=forms.Textarea
)


user_detail.html



<div class="col-lg-4 text-center p-5" style="padding-right:20px; border-right: 1px solid #e5e5e5;">
<a href="{% url 'portal:contact_form' kwarg.user.email %}">
<h1><i class="fa fa-2x fa-envelope-o text-success" aria-hidden="true"></i></h1>
<p class="text-muted mt-3 ">Contact Customer</p>
</a>
</div>






django forms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 1:12









Dominic M.Dominic M.

9119




9119













  • Why not use request.session ? or you wanna look into how to pass variables with kwargs

    – nara_l
    Nov 22 '18 at 1:32



















  • Why not use request.session ? or you wanna look into how to pass variables with kwargs

    – nara_l
    Nov 22 '18 at 1:32

















Why not use request.session ? or you wanna look into how to pass variables with kwargs

– nara_l
Nov 22 '18 at 1:32





Why not use request.session ? or you wanna look into how to pass variables with kwargs

– nara_l
Nov 22 '18 at 1:32












1 Answer
1






active

oldest

votes


















1














There are multiple ways to do that. Like for example:



Method 1: Send it in URL Querystring



from you previous details view, send the email in url querystring. For example:



 def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
user_email = form.cleaned_data.get('user_email') # assuming its the form field you are using for catching user email

redirect_url = "{}?user_email={}".format(self.get_success_url(), user_email)
return HttpResponseRedirect(redirect_url)


And catch it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.GET.get('user_email')})
...


Method 2: Store in session.



Its almost similar to last approach, but it stores the data in session.



def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
request.session['user_email'] = form.cleaned_data.get('user_email') # Storing data in session

# every other code


And get it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.session.get('user_email')})
...


Update



If you are coming from a detail page, means you have the access to the object. So in the detail page, update the contact button like this:



<a href="{% url 'portal:contact_form' object.email %}">contact</a>  // or user.email if you are passing user object as user in context


in Contact View:



def Contact(request, email):
if request.method == "GET":
form = ContactForm(initial={'contact_email': email)})





share|improve this answer


























  • Why are you using a form? Do i have to use a form to pass the variable to the next view?

    – Dominic M.
    Nov 22 '18 at 17:18











  • How are you planning to capture user email if you are not using any form or input method?

    – ruddra
    Nov 22 '18 at 18:28











  • Isn't the user detail page already querying the data for that specific user?

    – Dominic M.
    Nov 22 '18 at 18:30











  • Ah, that makes things easier. Please see my updated answer

    – ruddra
    Nov 22 '18 at 18:45











  • This has taught me a lot, thank you very much

    – Dominic M.
    Nov 23 '18 at 1:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422600%2fpassing-an-initial-value-into-a-form-field-from-previous-detail-view%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There are multiple ways to do that. Like for example:



Method 1: Send it in URL Querystring



from you previous details view, send the email in url querystring. For example:



 def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
user_email = form.cleaned_data.get('user_email') # assuming its the form field you are using for catching user email

redirect_url = "{}?user_email={}".format(self.get_success_url(), user_email)
return HttpResponseRedirect(redirect_url)


And catch it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.GET.get('user_email')})
...


Method 2: Store in session.



Its almost similar to last approach, but it stores the data in session.



def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
request.session['user_email'] = form.cleaned_data.get('user_email') # Storing data in session

# every other code


And get it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.session.get('user_email')})
...


Update



If you are coming from a detail page, means you have the access to the object. So in the detail page, update the contact button like this:



<a href="{% url 'portal:contact_form' object.email %}">contact</a>  // or user.email if you are passing user object as user in context


in Contact View:



def Contact(request, email):
if request.method == "GET":
form = ContactForm(initial={'contact_email': email)})





share|improve this answer


























  • Why are you using a form? Do i have to use a form to pass the variable to the next view?

    – Dominic M.
    Nov 22 '18 at 17:18











  • How are you planning to capture user email if you are not using any form or input method?

    – ruddra
    Nov 22 '18 at 18:28











  • Isn't the user detail page already querying the data for that specific user?

    – Dominic M.
    Nov 22 '18 at 18:30











  • Ah, that makes things easier. Please see my updated answer

    – ruddra
    Nov 22 '18 at 18:45











  • This has taught me a lot, thank you very much

    – Dominic M.
    Nov 23 '18 at 1:57
















1














There are multiple ways to do that. Like for example:



Method 1: Send it in URL Querystring



from you previous details view, send the email in url querystring. For example:



 def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
user_email = form.cleaned_data.get('user_email') # assuming its the form field you are using for catching user email

redirect_url = "{}?user_email={}".format(self.get_success_url(), user_email)
return HttpResponseRedirect(redirect_url)


And catch it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.GET.get('user_email')})
...


Method 2: Store in session.



Its almost similar to last approach, but it stores the data in session.



def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
request.session['user_email'] = form.cleaned_data.get('user_email') # Storing data in session

# every other code


And get it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.session.get('user_email')})
...


Update



If you are coming from a detail page, means you have the access to the object. So in the detail page, update the contact button like this:



<a href="{% url 'portal:contact_form' object.email %}">contact</a>  // or user.email if you are passing user object as user in context


in Contact View:



def Contact(request, email):
if request.method == "GET":
form = ContactForm(initial={'contact_email': email)})





share|improve this answer


























  • Why are you using a form? Do i have to use a form to pass the variable to the next view?

    – Dominic M.
    Nov 22 '18 at 17:18











  • How are you planning to capture user email if you are not using any form or input method?

    – ruddra
    Nov 22 '18 at 18:28











  • Isn't the user detail page already querying the data for that specific user?

    – Dominic M.
    Nov 22 '18 at 18:30











  • Ah, that makes things easier. Please see my updated answer

    – ruddra
    Nov 22 '18 at 18:45











  • This has taught me a lot, thank you very much

    – Dominic M.
    Nov 23 '18 at 1:57














1












1








1







There are multiple ways to do that. Like for example:



Method 1: Send it in URL Querystring



from you previous details view, send the email in url querystring. For example:



 def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
user_email = form.cleaned_data.get('user_email') # assuming its the form field you are using for catching user email

redirect_url = "{}?user_email={}".format(self.get_success_url(), user_email)
return HttpResponseRedirect(redirect_url)


And catch it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.GET.get('user_email')})
...


Method 2: Store in session.



Its almost similar to last approach, but it stores the data in session.



def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
request.session['user_email'] = form.cleaned_data.get('user_email') # Storing data in session

# every other code


And get it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.session.get('user_email')})
...


Update



If you are coming from a detail page, means you have the access to the object. So in the detail page, update the contact button like this:



<a href="{% url 'portal:contact_form' object.email %}">contact</a>  // or user.email if you are passing user object as user in context


in Contact View:



def Contact(request, email):
if request.method == "GET":
form = ContactForm(initial={'contact_email': email)})





share|improve this answer















There are multiple ways to do that. Like for example:



Method 1: Send it in URL Querystring



from you previous details view, send the email in url querystring. For example:



 def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
user_email = form.cleaned_data.get('user_email') # assuming its the form field you are using for catching user email

redirect_url = "{}?user_email={}".format(self.get_success_url(), user_email)
return HttpResponseRedirect(redirect_url)


And catch it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.GET.get('user_email')})
...


Method 2: Store in session.



Its almost similar to last approach, but it stores the data in session.



def DetailForm(request, *args, **kwargs):
...
if form.is_valid():
# do some things
request.session['user_email'] = form.cleaned_data.get('user_email') # Storing data in session

# every other code


And get it in Next form view:



  def Contact(request):
form_class = ContactForm(initial={'contact_email': request.session.get('user_email')})
...


Update



If you are coming from a detail page, means you have the access to the object. So in the detail page, update the contact button like this:



<a href="{% url 'portal:contact_form' object.email %}">contact</a>  // or user.email if you are passing user object as user in context


in Contact View:



def Contact(request, email):
if request.method == "GET":
form = ContactForm(initial={'contact_email': email)})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 18:45

























answered Nov 22 '18 at 3:01









ruddraruddra

14.8k32748




14.8k32748













  • Why are you using a form? Do i have to use a form to pass the variable to the next view?

    – Dominic M.
    Nov 22 '18 at 17:18











  • How are you planning to capture user email if you are not using any form or input method?

    – ruddra
    Nov 22 '18 at 18:28











  • Isn't the user detail page already querying the data for that specific user?

    – Dominic M.
    Nov 22 '18 at 18:30











  • Ah, that makes things easier. Please see my updated answer

    – ruddra
    Nov 22 '18 at 18:45











  • This has taught me a lot, thank you very much

    – Dominic M.
    Nov 23 '18 at 1:57



















  • Why are you using a form? Do i have to use a form to pass the variable to the next view?

    – Dominic M.
    Nov 22 '18 at 17:18











  • How are you planning to capture user email if you are not using any form or input method?

    – ruddra
    Nov 22 '18 at 18:28











  • Isn't the user detail page already querying the data for that specific user?

    – Dominic M.
    Nov 22 '18 at 18:30











  • Ah, that makes things easier. Please see my updated answer

    – ruddra
    Nov 22 '18 at 18:45











  • This has taught me a lot, thank you very much

    – Dominic M.
    Nov 23 '18 at 1:57

















Why are you using a form? Do i have to use a form to pass the variable to the next view?

– Dominic M.
Nov 22 '18 at 17:18





Why are you using a form? Do i have to use a form to pass the variable to the next view?

– Dominic M.
Nov 22 '18 at 17:18













How are you planning to capture user email if you are not using any form or input method?

– ruddra
Nov 22 '18 at 18:28





How are you planning to capture user email if you are not using any form or input method?

– ruddra
Nov 22 '18 at 18:28













Isn't the user detail page already querying the data for that specific user?

– Dominic M.
Nov 22 '18 at 18:30





Isn't the user detail page already querying the data for that specific user?

– Dominic M.
Nov 22 '18 at 18:30













Ah, that makes things easier. Please see my updated answer

– ruddra
Nov 22 '18 at 18:45





Ah, that makes things easier. Please see my updated answer

– ruddra
Nov 22 '18 at 18:45













This has taught me a lot, thank you very much

– Dominic M.
Nov 23 '18 at 1:57





This has taught me a lot, thank you very much

– Dominic M.
Nov 23 '18 at 1:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422600%2fpassing-an-initial-value-into-a-form-field-from-previous-detail-view%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules