Django 2.1.1 creating CharField in a Form in Django on a Raspberry pi

11 views
Skip to first unread message

kheinri...@gmail.com

unread,
Oct 9, 2018, 7:38:01 AM10/9/18
to Django users
hello,

I've tried to create a simple CharField in a Form and show it in my webbrowser
but I didn't see anything on my webbrowser?


Below my code:

forms.py
from django import forms



class  menuForm(forms.Form):
    image
= forms.CharField(initial = 'Your Name')
    date
= forms.DateField()
   

   


urls.py
from django.urls import path
from . import views

urlpatterns
= [
    path
('', views.menu),    

]

Views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import menuForm

def menu(request):
    frm
= menuForm()
   
   
return  render(request, 'base.html',  {' form ': frm} )
   


html-file

<html lang="de">

<head>
   
<title> Menu</title>
   
</head>
<body>
Hallo
   
<p>{{ frm }}</p>
   
</body>
</html>

I can only see "Hallo" on my webbrowser but no input for a CharField?

Can anybody help?


Joel

unread,
Oct 9, 2018, 7:54:21 AM10/9/18
to django...@googlegroups.com
instead of  <p>{{ frm }}</p>, try:

 <p>{{ form }}</p>

I think you'll understand what you did wrong.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/abc9ba3c-1d3b-4d84-9915-16c4fc02a27e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

konstantin Heinrich

unread,
Oct 9, 2018, 8:03:25 AM10/9/18
to django...@googlegroups.com
I declared in views.py variable called frm so I have to use it
in my html file to have access to that file.?



views.py

Joel

unread,
Oct 9, 2018, 8:19:10 AM10/9/18
to django...@googlegroups.com
You passed form to template, so you have to refer to that.

konstantin Heinrich

unread,
Oct 9, 2018, 8:29:26 AM10/9/18
to django...@googlegroups.com
I replaced code with <p>{{ form }}</p> but nothing changed.?

Michal Petrucha

unread,
Oct 9, 2018, 8:39:32 AM10/9/18
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi,

On Tue, Oct 09, 2018 at 02:28:25PM +0200, konstantin Heinrich wrote:
> I replaced code with <p>{{ form }}</p> but nothing changed.?

There's one more error in your code, and it's right here:

> return render(request, 'base.html', {' form ': frm} )

As you can see above, you're passing a context variable by the name of
“ form ” with a leading an trailing space to the template engine. As
far as I know, context variables cannot contain spaces, because there
is no way to actually refer to them from the template itself.
Honestly, I'm surprised that doesn't raise an error.

Regardless, you should remove the spaces around “form”, and then
you'll be able to refer to the form as, well, “form” in your template.

Good luck,

Michal
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCgAGBQJbvKE4AAoJEHA7T/IPM/klQToP/0+5qNMWutLRDIkvYdXAz/iQ
rTfPg36VcKJzgdHz2ncRvl9wYNhxyeyGqYS/FjQr8ZyqkMFsL89UfamSEDdwkJiP
BOVm4IWrVdPoANRqtaklmChBHMbFCAsDrj+RY6UH9nZDTb5yiKraRHfKNFadJ4GA
LimrIXMbcPJ/2hzC+imv68qsR81WOeIdSSNrY4TnyJrzVp0qgZaiWdQ8YCmG0Tbc
S71nNvUUfbNCp2kCiu/pH0wLscMXBJlSVJ5BtzGN99gK9LusSUKWJ/mqLm7w9DCS
k2Z0xCgQ8NwaNRvtkRsl3TEHIpAfac3vf92/VEuWsOGWpCb8d8KYCwVzsf57J9Kf
6QCP9pw9RP8B3JMIm3I0djiakA3UTZNfbog3aQGzLVROmuQjUyWwvxVw2vaqUhj2
moL/Eq/M6eahcfYn3CFYfLojqukXDS0pL1Qgj+Njqng2qiG5DKAIF1iMFZZoREs4
OOZC+dJg3Vos/ebY/vZBYdAxO92UsKYXod5WCqNaoyyKuwWaufoHhNZoteVZ5hjj
F6GGcGP6ugdswmZDT1E3+RnLgFsmB8G8+w3uAi8obFDhFjkldgfVxEnw0Ki2JcTB
UWkD1KCur9f7hYo+34vNNfiCtXlxACaMdVNdb3X1R5aCMvh5VjvBJI/31ja6Yw9Z
K3cucs2jnkLdbf9KiSZD
=aClS
-----END PGP SIGNATURE-----

konstantin Heinrich

unread,
Oct 9, 2018, 8:49:45 AM10/9/18
to django...@googlegroups.com
Hi Michal,

yes that was the error.
Thank you so much.
Strange that no error was shown.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages