Django - 不向DataBase添加新数据

时间:2017-09-13 13:01:08

标签: python mysql django web

所以我正在建立我的第一个网站,我遇到了问题。 我真的不明白视图,模块和我的网站如何连接.. 也许这就是我收到这个错误的原因......

我的html文件夹是map / templates / serverlist.html

我正在尝试将我的数据添加到数据库中,每次在网站中我点击提交按钮我看到一个POST请求,但数据本身没有显示在网站和/或MySQL数据库中。

知道缺少什么吗?

IndentationError at /
unexpected indent (forms.py, line 6)
Request Method: GET
Request URL:    http://172.16.10.60:8000/
Django Version: 1.3.1
Exception Type: IndentationError
Exception Value:    
unexpected indent (forms.py, line 6)
Exception Location: /media/sf_C_DRIVE/Users/eilon.ashkenazi/Desktop/EilonA/DevOpsMap/WebFiles/../WebFiles/map/views.py in <module>, line 4
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Python Path:    
['/media/sf_C_DRIVE/Users/eilon.ashkenazi/Desktop/EilonA/DevOpsMap/WebFiles',
 '/usr/lib64/python27.zip',
 '/usr/lib64/python2.7',
 '/usr/lib64/python2.7/plat-linux2',
 '/usr/lib64/python2.7/lib-tk',
 '/usr/lib64/python2.7/lib-old',
 '/usr/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages']
Server time:    Wed, 13 Sep 2017 07:39:50 -0500

view.py

# Create your views here.
from django.shortcuts import render_to_response
from django.template import RequestContext
from map.forms import PostForm
from map.models import serverlist

def home(request):
    entries = serverlist.objects.all()
    return render_to_response('serverlist.html', {'serverlist' : entries })
def postView(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid(): # Checks if validation passed

                servername = request.POST.get('ServerName','')
                owner = request.POST.get('Owner','')
                item = serverlist(servername=ServerName,owner=Owner)
                form.save() # Save the data into the DB
                return HttpRespondRedirect(reverse('map:serverlist')) # Resdirect after POST
        else:
            form = PostForm()

    return render(request, 'templates/serverlist.html', { 'form' : form, })

forms.py

from django import forms
from map.models import serverlist

class PostForm(forms.Form):
    ServerName = forms.CharField(max_length = 30)
    Owner = forms.CharField(max_length = 50)
#   Project = forms.CharField(max_length = 30)
#   Description = forms.CharField(max_length = 255)
#   IP = forms.CharField(max_length = 30)
#   ILO = forms.CharField(max_length = 30)
#   Rack = forms.CharField(max_length = 30)
#   Status = forms.CharField(max_length = 30)

的index.html

<html lang="en">

<head>
     <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">DevOps Map</a>
    </div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Servers</a></li>
      <li><a href="#">Switches</a></li>
    </ul>
  </div>
</nav>
</head>

<body>


<div class="container">
    <br>
    <br>
    <center><h1>DevOps Server List</h1></center>
    <br>
    <br>
    <br>
    <table class="table table-hover">
      <h5>
    <thread>

      <tr>
        <th> Server Name </th>
        <th> Owner </th>
        <th> Project </th>
        <th> Description </th>
        <th> IP Address </th>
        <th> ILO </th>
        <th> Rack </th>
        <th> Status </th>


    </tr>
    </thread>
    <tbody>

                {% for server in serverlist %}
        <tr>
                <div class ="server">
            <td>{{ server.ServerName }}</td>
            <td>{{ server.Owner }}</td>
            <td>{{ server.Project }}</td>
            <td>{{ server.Description }}</td>
            <td>{{ server.IP }}</td>
            <td>{{ server.ILO }}</td>
            <td>{{ server.Rack }}</td>
            <td>{{ server.Status }}</td>
            <td>

            <div class="dropdown">
                <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Actions
                <span class="caret"></span></button>
                <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Edit</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Delete</a></li>
            </td>
                </div>
        </tr>
                {% endfor %}
    </tbody>
</h5>
    </table>

    <br>
    <br>
  <form method="post">

    <p><label for="ServerName">Server Name:</label><input type="text" name="ServerName" id="ServerName" /></p>

    <p><label for="Owner">Owner:</label><input type="text" name="Owner" id="Owner" /></p>

       <center><h2> <button type="submit" class="save btn btn-default">Add Item</button></h2></center>
   </form>
</div>

</body>

</html>

2 个答案:

答案 0 :(得分:2)

例外情况unexpected indent (forms.py, line 6)

我认为第6行是# Project = forms.CharField(max_length = 30) 看起来不错,但实际代码中的上一行或下一行可能存在缩进错误。

答案 1 :(得分:0)

您的缩进似乎与views.py文件中的第14-19行不同。尝试用标签(或4个空格)推回每一行。