Please I am still left with the AttributeError, The form is now publishing fully and very well but it cannot save to the MySQL table. below is the error details and the recent code of view.py AttributeError at /namev/
'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 CODE FOR VIEW.PY ef Namev_view (request): if request.method == 'POST': form = NameForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/namev/') else: form = NameForm() return render(request, 'namev.html', {'form': form}) Dr. Chielo C. Ifeanyi Chief Programmer, Head Webometrics Section ICT Unit, UNN 08032366433, 08154804230 ifeanyi.chi...@unn.edu.ng http://www.unn.edu.ng/users/ifeanyichielo <http://www.unn.edu.ng> On Sat, Apr 4, 2020 at 3:21 PM Luqman Shofuleji <luqmans...@gmail.com> wrote: > Okay, now the new error is that is not seeing that 'form' for the get > request, > you need to also add form = NameForm () before the if statement "if > request.method == 'POST' " to handle the page get request. > > > > On Sat, Apr 4, 2020, 12:33 PM Ifeanyi Chielo <ifeanyi.chi...@unn.edu.ng> > wrote: > >> Thanks a lot, >> I did as you said and I encountered the error message below: >> UnboundLocalError at /namev/ >> >> 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 >> Dr. Chielo C. Ifeanyi >> Chief Programmer, >> Head Webometrics Section >> ICT Unit, UNN >> 08032366433, 08154804230 >> ifeanyi.chi...@unn.edu.ng >> http://www.unn.edu.ng/users/ifeanyichielo <http://www.unn.edu.ng> >> >> >> >> On Sat, Apr 4, 2020 at 10:09 AM Luqman Shofuleji <luqmans...@gmail.com> >> wrote: >> >>> In def Namev_view under views.py, include form in the return parameter >>> and see if that resolves the issue >>> >>> return render(request, 'namev.html', {'form':form}) >>> >>> >>> >>> On Sat, Apr 4, 2020, 1:35 AM Ifeanyi Chielo <ifeanyi.chi...@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 >>>> >>>> [image: django form.png] >>>> >>>> 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+unsubscr...@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 >>>> <https://groups.google.com/d/msgid/django-users/f50577dd-d5c0-4478-8993-7bab533f24f9%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >>> 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+unsubscr...@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 >>> <https://groups.google.com/d/msgid/django-users/CAHyB84rUTDyiba%2B%2Bvk%3DJFuRN7CPBPTpso2JvyBc28Q4gG-faYg%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> 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+unsubscr...@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 >> <https://groups.google.com/d/msgid/django-users/CAOcS8H1YiVcABEXBEa5-qEu4V4NBeGfY3PkFa%3DYCf9OEdUAO-Q%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAHyB84oveTrACB7s%3DrDQKY7xtfEiHhYZAokZYxo8h%2BiAc1kWNw%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAHyB84oveTrACB7s%3DrDQKY7xtfEiHhYZAokZYxo8h%2BiAc1kWNw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H0fNFvDrnoCcSdDjFiGiEab56jJnqviqde0G7i_k7XZjQ%40mail.gmail.com.