View.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .models import Namerec
from .forms import NameForm
from django.views.generic import TemplateView
def HomePageView (request, *args, **kwargs):
return render(request,"home.html", {})
def Namev_view (request):
if request.method == 'POST':
form = NameForm (request.POST)
if NameForm.is_valid():
NameForm.save()
context = {
'form': form
}
return render(request, 'namev.html', {})
form.py
from .models import Namerec
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
template (namev.htlm)
<form action="/namev/" method="Post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
Model.py
from django.db import models
class Namerec(models.Model):
your_name = models.CharField(max_length=30)
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f50577dd-d5c0-4478-8993-7bab533f24f9%40googlegroups.com.
In the form template tag, shouldn’t it be {{ form.as_p}} tag?
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Please share code for home.html & urls.py. Also have you included the app name in the settings.py?
Thanks,
Bruckner de Villiers
083 625 1086
From: <django...@googlegroups.com> on behalf of Ifeanyi Chielo <ifeanyi...@unn.edu.ng>
Reply to: <django...@googlegroups.com>
Date: Saturday, 04 April 2020 at 02:51
To: Django users <django...@googlegroups.com>
Subject: Re: Working with forms
Thanks Victor,
I did it and it does not make any cnange
On Saturday, April 4, 2020 at 1:41:19 AM UTC+1, victor awakan wrote:
In the form template tag, shouldn’t it be {{ form.as_p}} tag?
On Sat 4. Apr 2020 at 3.35, Ifeanyi Chielo <ifeany...@unn.edu.ng> wrote:
Hello,I developed a form with a single field, but this form displays only the submit button at the browser as shown in the image below. Please how can I correct this and also save the field to the MySQL table. My code is also shown below
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9825aec7-7e87-4d4a-80f9-634cb9e4dd59%40googlegroups.com.
View
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .models import Namerec
from .forms import NameForm
from django.views.generic import TemplateView
def HomePageView (request, *args, **kwargs):
return render(request,"home.html", {})
project url
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
App url
urlpatterns = [
path('', HomePageView, name = 'home'),
path('namev/', Namev_view),
#path('', HomePageView),
#path('namev/', Nameview_view),
]
Note, I have not written any code at home.html template and it is rendering well
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/E47A5C26-9855-4ED7-86AD-3F427A2E88B7%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/E47A5C26-9855-4ED7-86AD-3F427A2E88B7%40gmail.com.
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f50577dd-d5c0-4478-8993-7bab533f24f9%40googlegroups.com.
local variable 'form' referenced before assignment
Request Method: | GET |
---|---|
Request URL: | http://localhost:8000/namev/ |
Django Version: | 2.1.5 |
Exception Type: | UnboundLocalError |
Exception Value: | local variable 'form' referenced before assignment |
Exception Location: | C:\Users\IFEANYI CHIELO\divinecrownapp\pages\views.py in Namev_view, line 42 |
Python Executable: | C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\python.exe |
Python Version: | 3.7.2 |
Python Path: | ['C:\\Users\\IFEANYI CHIELO\\divinecrownapp', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'] |
Server time: | Sat, 4 Apr 2020 11:27:07 +0000 |
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHyB84rUTDyiba%2B%2Bvk%3DJFuRN7CPBPTpso2JvyBc28Q4gG-faYg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H1YiVcABEXBEa5-qEu4V4NBeGfY3PkFa%3DYCf9OEdUAO-Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANoHCsuxMoGCQ9GNi5xa%2BYoNLnE8dwKMuh%2BXCvG_swxaZ_uT6g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H1YiVcABEXBEa5-qEu4V4NBeGfY3PkFa%3DYCf9OEdUAO-Q%40mail.gmail.com.
The view pages.views.Namev_view didn't return an HttpResponse object. It returned None instead.
Request Method: | GET |
---|---|
Request URL: | http://localhost:8000/namev/ |
Django Version: | 2.1.5 |
Exception Type: | ValueError |
---|---|
Exception Value: | The view pages.views.Namev_view didn't return an HttpResponse object. It returned None instead. |
Exception Location: | C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response, line 137 |
Python Executable: | C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\python.exe |
---|---|
Python Version: | 3.7.2 |
Python Path: | ['C:\\Users\\IFEANYI CHIELO\\divinecrownapp', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'] |
Server time: | Sat, 4 Apr 2020 15:27:13 +0000 |
---|
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHyB84oveTrACB7s%3DrDQKY7xtfEiHhYZAokZYxo8h%2BiAc1kWNw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H0L0xJCtBNKqr0pmWBT_UkpwJcDxDeK5OKTEtkGnwC6rA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHyB84phLjKzWfBw8Hbjw05K8tWNmW3bdFFQJwfOG%3DhC5R6Evg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H2XeZcfr-OfOe9v8kvb5ysWW5L2mt-dknFGKrY3UgACQA%40mail.gmail.com.
local variable 'context' referenced before assignment" I then tied to correct the error by adding a line of code: context = {'form': form} then only the submit button was rendered again at the browser, without the complete form as shown below
def Namev_view (request):form = NameForm()
context = {'form':form}
# if this is a POST request we need to process the form dataif request.method == 'POST':# create a form instance and populate it with data from the request:form = NameForm (request.POST)# check whether it's valid:if NameForm.is_valid():NameForm.save()context = {'form': form}
return render(request, 'namev.html', context)# return render(request, 'namev.html', {'form':form})
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANoHCsvvsdb5-cU3GtFnFZ1RMCQDP0Go8037Kw5NqQDXDzh-GA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H2xzdebvtkrO94kRX4gVkxMS%3Dmg%2BJ5g5Ga9x6kqLCyXZQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H2xzdebvtkrO94kRX4gVkxMS%3Dmg%2BJ5g5Ga9x6kqLCyXZQ%40mail.gmail.com.
type object 'NameForm' has no attribute 'save'
Request Method: | POST |
---|
Request URL: | http://localhost:8000/namev/ |
---|---|
Django Version: | 2.1.5 |
Exception Type: | AttributeError |
---|---|
Exception Value: | type object 'NameForm' has no attribute 'save' |
Exception Location: | C:\Users\IFEANYI CHIELO\divinecrownapp\pages\views.py in Namev_view, line 16 |
Python Executable: | C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\python.exe |
---|---|
Python Version: | 3.7.2 |
Python Path: | ['C:\\Users\\IFEANYI CHIELO\\divinecrownapp', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'] |
Server time: | Sun, 5 Apr 2020 13:59:01 +0000 |
---|
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54d77f2f-58cf-455f-9b11-8857a5ffb32b%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H0wvTPnK1bmk6Q8XcKZ5R3BMxt_nq-EqkvFwUL1rB0n9Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANoHCsvTpDUzvjUksAq4EET011k1dRmTY4pUG_w8fFO26T9dng%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H0FBxYptR5ffgL-cAw08PvWBfEnchYXxAjnsfXhiuRuwQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEL9fCH%2B3SLVbqfmvHj1dyP-H4EWtZmoWz5U2k4dNTXxEson8g%40mail.gmail.com.
'NameForm' object has no attribute 'save'
Request Method: | POST |
---|
Request URL: | http://localhost:8000/namev/ |
---|---|
Django Version: | 2.1.5 |
Exception Type: | AttributeError |
---|---|
Exception Value: | 'NameForm' object has no attribute 'save' |
Exception Location: | C:\Users\IFEANYI CHIELO\divinecrownapp\pages\views.py in Namev_view, line 17 |
Python Executable: | C:\Users\IFEANYI CHIELO\AppData\Local\Programs\Python\Python37\python.exe |
---|---|
Python Version: | 3.7.2 |
Python Path: | ['C:\\Users\\IFEANYI CHIELO\\divinecrownapp', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\IFEANYI CHIELO\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\IFEANYI ' 'CHIELO\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'] |
Server time: | Sun, 5 Apr 2020 19:46:34 +0000 |
---|
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHyB84oveTrACB7s%3DrDQKY7xtfEiHhYZAokZYxo8h%2BiAc1kWNw%40mail.gmail.com.
class NameForm (forms.ModelForm)
class Meta:
model = Namerec
fields = ['your_name’,]
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f50577dd-d5c0-4478-8993-7bab533f24f9%40googlegroups.com.
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHyB84rUTDyiba%2B%2Bvk%3DJFuRN7CPBPTpso2JvyBc28Q4gG-faYg%40mail.gmail.com.
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H1YiVcABEXBEa5-qEu4V4NBeGfY3PkFa%3DYCf9OEdUAO-Q%40mail.gmail.com.
--
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...@googlegroups.com.
Port your code back to the initial state it was when you asked the question and on the template change{{form}} to {{form.as_p}}
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/063525f8-c177-47d6-b849-52240522ae4f%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H2%2BtMxewMDAm6s%3DAQB6L%2BYCMTUaPHpyrr5A20u4z1vP2w%40mail.gmail.com.