我在我的models.py
文件中添加了新的列,保存了我的代碼,然後在Terminal
我跑sudo python manage.py makemigrations music
,然後是sudo python manage.py migrate
。一旦運行sudo python manage.py migrate
,我得到了Terminal
一個錯誤說:爲什麼我的數據庫不能工作?
這是我的index.html
文件:
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
{% load static %}
<link rel="stylesheet" href="{% static 'index.css' %}">
</head>
<body>
<form class="col-md-4 col-md-offset-4" method="POST" name="formHandler">
{% csrf_token %}
<div class="userNameMovement">
<label for="usr">Email:</label>
<input class="form-control" id="email" name="email" placeholder="email" required="required">
</div>
<div class="passwordMovement">
<label for="usr">Username:</label>
<input class="form-control" id="userName" name="userName" placeholder="Username" required="required">
<button type="submit" class="btn btn-default">Submit</button>
</form>
</body>
</html>
這是我的models.py
文件:
from django.db import models
class Person(models.Model):
email = models.CharField(max_length=20, default=4)
userName = models.CharField(max_length=20, default=4)
def __str__(self):
return self.email + " - " + self.userName
class UI(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
這是我的views.py
文件:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Person
def index(request):
if request.method == 'POST':
email = request.POST.get('email')
userName = request.POST.get('userName')
if email and userName:
user = Person.objects.create(email=email, username=userName)
user.save()
return render(request, 'music/index.html')
def detail(request, user_id): # Testing out page 2
return HttpResponse("<h2>Page # (testing this out) " + str(user_id) + "</h2>")
你當然不應該將你的遷移作爲'sudo'運行。 –
@DanielRoseman我試圖運行它沒有sudo,仍然無法正常工作。我不能'.save()'終端中的任何Person對象而不會出現錯誤 – bojack
在其他遷移文件中,您是否看到任何使用電子郵件字段的遷移? –