Saturday, April 4, 2020

Re: Working with forms

Thanks a lot, I believe I am close to the result. I did exactly what you instructed and another error appeared thus:

ValueError at /namev/

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
Meanwhile the current code for the view is shown below:
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):
    form = NameForm ()
    # if this is a POST request we need to process the form data
    if 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', {'form':form})

Dr. Chielo C. Ifeanyi
Chief Programmer,
Head Webometrics Section
ICT Unit, UNN


On Sat, Apr 4, 2020 at 3:21 PM Luqman Shofuleji <luqmanshof@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.chielo@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


On Sat, Apr 4, 2020 at 10:09 AM Luqman Shofuleji <luqmanshof@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.chielo@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


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+unsubscribe@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-users+unsubscribe@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-users+unsubscribe@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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHyB84oveTrACB7s%3DrDQKY7xtfEiHhYZAokZYxo8h%2BiAc1kWNw%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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOcS8H0L0xJCtBNKqr0pmWBT_UkpwJcDxDeK5OKTEtkGnwC6rA%40mail.gmail.com.

No comments:

Post a Comment