Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

css - How to render html form in Django

As I am trying to make use bootstrap file in my django application I am not able to make connections my models I am able to see the bootstrap theme when I run server but when I enter data and click on submit button data is not storing in my database and it is not visible in admin panel also.

(When I use simple html file without bootstrap I am able to save the data in database and it is also showing in admin panel)

**my models.py**

from django.db import models
from django.contrib.auth.models import User

class student(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)
    phone = models.CharField(max_length=25)
    message = models.TextField(max_length=500)


    def __str__(self):
        return self.email


**my views.py**

from django.shortcuts import render
from .models import *
from .forms import *
from django.http import *

def contact(request):
    if request.method == 'POST':

        form = details_forms(request.POST)

        name = request.POST.get('name', '')#name model
        email = request.POST.get('email', '')#email model
        phone = request.POST.get('phone', '')#phone model
        message = request.POST.get('message', '')#message model
        details_obj = student(name=name,email=email,phone=phone,message=message)
        details_obj.save()
        form = details_forms()



    else:
        form = details_forms()
    return render(request,'contact.html',{'form':form})


**mycontact.html file**

{% load static from staticfiles %}

<!DOCTYPE html>
<html lang="en">

  <head>
    <style>
      body{
        background: url('{% static "img/new4.jpg" %}') no-repeat center fixed;
        - webkit- background: cover;
        - moz - background: cover;
        - o - background: cover;
              background-size: cover;
      }

      </style>


    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Personality Leading.com</title>

    <!-- Bootstrap core CSS -->
    <link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
    <!-- <link rel="stylesheet" type="text/css" href="{% static 'vendor/bootstrap/css/Custom' %}"> -->
    <link rel="stylesheet" type="text/css" href="{% static 'vendor/bootstrap/css/Custom.css' %}">

    <!-- Custom fonts for this template -->
    <link href="{% static 'vendor/font-awesome/css/font-awesome.min.css' %}" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

    <!-- Custom styles for this template -->
    <link href="{% static 'css/clean-blog.min.css' %}" rel="stylesheet">

  </head>

  <body>

<!-- Navigation -->
<div class="mynav">
  <div class="p-3 mb-2 bg-info text-white"><strong> PERSONALITY LEADING </strong></div>
  <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
      <div class="container">
        <a class="navbar-brand" href="index.html"></a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          Menu
          <i class="fa fa-bars"></i>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item">
              <a class="nav-link" href="/home/">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/about/">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/services/">Services</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="/contact/">Contact</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
  </div>
</div>

    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>


    <!-- Page Header -->
    <!-- <header class="masthead" style="background-image: url('img/contact-bg.jpg')"> -->
      <div class="overlay"></div>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <div class="page-heading">
              <h1>Contact Us</h1>
              <span class="subheading">Have questions? I have answers.</span>
            </div>
          </div>
        </div>
      </div>
    </header>


    <!-- Main Content -->
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 text-dark mx-auto">
          <p>Want to get in touch? Fill out the form below to send us a message and We will get back to you as soon as possible!</p>
          <form name="sentMessage" id="contactForm" novalidate>
            <form method="post" action="">
            {% csrf_token %}
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">

                 <div class="form-group">
                <label>Name</label>
                <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                <div class="col-md-4">
                  {{ form.name}}
                <p class="help-block text-danger"></p>


              </div>
            </div>
            <div class="control-group">

              <div class="form-group floating-label-form-group controls">
                <div class="form-group">
                <label>Email Address</label>
                <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                <div class="col-md-4">
                  {{ form.email}}
                <p class="help-block text-danger"></p>


              </div>
            </div>
            <div class="control-group">
              <div class="form-group">
              <div class="form-group col-xs-12 floating-label-form-group controls">
                <label>Phone Number</label>
                <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                <div class="col-md-4">
                  {{ form.phone}}
                <p class="help-block text-danger"></p>


              </div>
            </div>
            <div class="control-group">
              <div class="form-group">
              <div class="form-group floating-label-form-group controls">
                <label>Message</label>
                <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                <div class="col-md-4">
                  {{ form.message}}
                <p class="help-block text-danger"></p>


              </div>
            </div>
            <br>
            <div id="success"></div>
            <div class="form-group">
              <button type="submit" class="btn btn-primary" id="sendMessageButton">Send</button>

            </div>
            </form>
          </form>
        </div>
      </div>
    </div>
    <div id="templatemo-contact">

            <p class="text-primary">
              <h7><strong>Meet us<span> at our location</span> </strong></h7> </p>
            <br>


                <div class="contactHldr text-#0085a1">
                    <ul>
                        <li>

                          <strong>Personality Leading</strong><br>
                            (Online Jobs Guru Pvt.ltd)<br>
                            #169, 13th Main 1st Floor<br>
                            Aicoboo Nagar, 1st stage, BTM Layout<br>
                          Bangalore - 560029</p>
                          <h4>Locate us on Google maps</h4>
                          <a href="https://www.google.co.in/maps/place/Onlinejobsguru+Pvt.Ltd./@12.9167255,77.6087831,17z/data=!3m1!4b1!4m5!3m4!1s0x3bae14fc2c1a4461:0xa948622b989d82ff!8m2!3d12.9167203!4d77.6109718?hl=en"> <img src="{%static 'img/map.png' %}" width="150" height="114"><br>
                            <br>

                            <!-- <h5>Phone: 080 - 41250340</h5> -->
                        <p class="text-primary"><strong>Phone:</strong> 080 - 41250340</p>
                        <p class="text-light"><strong>Email:</strong> <br> <a href="mailto:[email protected]">[email protected]</a><br><a href="mailto:[email protected]">[email protected]</a></p>

                          </a>
                        </li>



    <hr>

    <!-- Footer -->
    <footer>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <ul class="list-inline text-center">
              <li class="list-inline-item">
                <a href="https://twitter.com/onlinejobsguruu">
                  <span class="fa-stack fa-lg">
                    <i class="fa fa-circle fa-stack-2x"></i>
                    <i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
                  </span>
                </a>
              </li>
              <li class="list-inline-item">
                <a href="https://www.facebook.com/onlinejobsguru05/">
                  <span class="fa-stack fa-lg">
                    <i class="fa fa-circle fa-stack-2x"></i>
                    <i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
                  </span>
                </a>
              </li>
              <!-- <li class="list-inline-item">
                <a href="#">
                  <span class="fa-stack fa-lg">
                    <i class="fa fa-circle fa-stack-2x"></i>
                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                  </span>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if you are using ModelForm for that Model change your view to this

def contact(request):
    if request.method == 'POST':

        form = details_forms(request.POST)
        if form.is_valid():
           form.save()
           return render(request,'contact.html',{'form':form})
    else:
        form = details_forms()
    return render(request,'contact.html',{'form':form})

change this in your form

class Meta(): model = student fields = ['name','email','phone','message']

to

class Meta:
      model = student
      fields = ['name','email','phone','message']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...