Home > other >  Get method not changing to post in django forms
Get method not changing to post in django forms

Time:10-08

I have studied Django for 3 months but still I couldn't find the solution down below. I am building resume maker online website. But when I run function in views.py, I get method 'GET' every time, but I expected it should be POST, I think there is a problem with frontend. I have an awesome app called Nicepage which generates beautiful html codes for me without coding. I downloaded its source code and used it as a frontend for my website. I don't know why but, still I couldn't solve the problem. Can you please help me to solve this?

Here's the frontend:

<section  id="sec-800b">
<div >
    <div  data-image-width="360"
         data-image-height="360">
        <div >
            <h2 >Let's start with&nbsp;<br>personal information
            </h2>
            <div >
                <form enctype="multipart/form-data" method="POST"
                      
                      style="padding: 10px;">
                    {% csrf_token %}
                    <div >
                        {{ form.as_p }}
                        <!--                            <label for="name-86ce" >Fullname</label>-->
                        <!--                            <input type="text" placeholder="Enter your fullname carefully..." id="name-86ce" name="name"-->
                        <!--                                    required>-->
                    </div>
                    <div >
                        <label for="email-86ce" >Email</label>
                        <input type="email" placeholder="Enter a valid email address" id="email-86ce" name="email"
                                required="">
                    </div>
                    <div >
                        <label for="phone-9974" >Phone</label>
                        <input type="tel"
                               pattern="\ ?\d{0,3}[\s\(\-]?([0-9]{2,3})[\s\)\-]?([\s\-]?)([0-9]{3})[\s\-]?([0-9]{2})[\s\-]?([0-9]{2})"
                               placeholder="Enter your phone (e.g.  14155552675)" id="phone-9974" name="phone"
                                required="">
                    </div>
                    <div >
                        <label for="text-d550" >Address</label>
                        <input type="text" placeholder="enter your address..." id="text-d550" name="text"
                               >
                    </div>
                    <div >
                        <label for="text-10ad" >Postal code</label>
                        <input type="text" placeholder="ex. New York - 10001" id="text-10ad" name="text-1"
                               >
                    </div>
                    <div >
                        <a href="{% url 'template' %}"
                           >Back<br>
                        </a>
                    </div>
                    <div > Thank you! Your message has been sent.
                    </div>
                    <div > Unable to send your message. Please fix
                        errors then try again.
                    </div>
                    <input type="hidden" value="" name="recaptchaResponse">
                    <input type="hidden" name="formServices" value="40a3b58402a1a2984520a20649102ef7">
                </form>
            </div>
            <a href="{% url 'work_experience' %}"
               
               data-animation-name="pulse" data-animation-duration="1000"
               data-animation-direction="">Next                </a>
            <img 
                 src="{% static 'images/images.jfif' %}" alt=""
                 data-image-width="201" data-image-height="251">
            <h4 >We ensure that your ​​personal&nbsp;<br>information won't store anywhere!
            </h4>
        </div>
    </div>
</div>

urls.py

from django.urls import path
from .views import (
    index,
    start_building,
    template_choice,
    testing,
    work_experience,
    education,
    skills_summary
)

prefix = 'v1/requests/'
urlpatterns = [
    path("", index, name="homepage"),
    path("test/", testing, name="testing"),
    path(prefix   "education/", education, name="education"),
    path(prefix   "skills_summary/", skills_summary, name="finish"),
    path(prefix   "work_experience/", work_experience, name="work_experience"),
    path(prefix   "template_choice/", template_choice, name="template"),
    path(prefix   "starter/", start_building, name="start_process"),
]

views.py

from django.shortcuts import render
from .forms import PersonalDetailsForm, SingleForm
from django.http import HttpResponseRedirect

personal_details = []


def index(request):
    a = "We have got more than 50000 users used our website!"
    return render(request, "Home.html", {"a": a})


def template_choice(request):
    return render(request, "template_choice.html", {})


def start_building(request):
    print("[INFO]: function start__building is working!")
    form = PersonalDetailsForm(request.POST)
    print(request.method)
    if request.method == "POST":
        print(True)
        if form.is_valid():
            fullname = form.cleaned_data["fullname"]
            print(fullname)
    return render(request, "Page-1.html", {"form": form})


def work_experience(request):
    return render(request, "Page-2.html", {})


def education(request):
    return render(request, "education.html", {})


def skills_summary(request):
    return render(request, "skills_summary.html", {})


def testing(request):
    form = PersonalDetailsForm(request.POST)
    if request.method == "POST":
        if form.is_valid():
            data = form.cleaned_data["fullname"]
            print(data)
    return render(request, "formsets.html", {"form": form})
def work_experience(request):
    return render(request, "Page-2.html", {})


def education(request):
    return render(request, "education.html", {})


def skills_summary(request):
    return render(request, "skills_summary.html", {})


def testing(request):
    form = PersonalDetailsForm(request.POST)
    if request.method == "POST":
        if form.is_valid():
            data = form.cleaned_data["fullname"]
            print(data)
    return render(request, "formsets.html", {"form": form})

forms.py 


from django import forms
 
 
class SingleForm(forms.Form):
     single = forms.CharField(required=False)
 
 
class MyForm(forms.Form):
     original_field = forms.CharField()
     extra_field_count = forms.CharField(widget=forms.HiddenInput())
 
     def __init__(self, *args, **kwargs):
         extra_fields = kwargs.pop('extra', 0)
 
         super(MyForm, self).__init__(*args, **kwargs)
         self.fields['extra_field_count'].initial = extra_fields
 
         for index in range(int(extra_fields)):
             # generate extra fields in the number specified via extra_fields
             self.fields['extra_field_{index}'.format(index=index)] = \
                 forms.CharField()
 
 
class PersonalDetailsForm(forms.Form):
    fullname = forms.CharField(
        max_length=50,
        required=False,
        widget=forms.TextInput(
            attrs={
                "class": "u-border-1 u-border-grey-30 u-input u-input-rectangle uwhite",
                "placeholder": "Enter your fullname carefully...",
                "id": "name-86ce",
            }
        )
    )

CodePudding user response:

May be this'll help

#you are putting POSTinfo into form before checking method
#Try the following example instead

def start_building(request)
    form = PersonalDetailsForm() #Not putting the data into it

    if request.method == 'POST':
        form = PersonalDetailsForm(request.POST) #put the data into it only if method is post
        #OTHER STUFF

CodePudding user response:

There in the html form

Before

 </form>

tag in the bottom lines add this

<input type="submit" value="theTextYouWant">
  • Related