记录软件工程课程项目开发时遇到的各种小问题(django)
1.python manage.py makemigrations 无效/无法检测出model的变化
在修改了models.py之后,我们想要更新数据库的表,使用了python manage.py makemigrations 命令之后,显示出"No changes detected",我们因此无法migrate这个数据库。
解决办法:
在makemigrations 后面增加APP的名字
比如说:
python manage.py makemigrations coder_market
这时就能检测出哪些表有变化了
2.无法使数据库在admin后台可视化
网上的方法大多是修改admin.py来修改,但是我使用这样的办法无效。发现将register的代码放到models.py时可以使用
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models
from django.contrib import admin
'''
class user_login(models.Model):
username = models.CharField(max_length=30)
password = models.CharField(max_length=30)
identity = models.CharField(max_length=15)
'''
'''
class identity(models.Model):
username = models.ForeignKey(User)
iden = models.CharField(max_length=10)
'''
class publisher(models.Model):
username = models.CharField(max_length=30)
order_project = models.IntegerField()
presonal_information = models.CharField(max_length=100)
accept_information = models.TextField()
class receiver(models.Model):
username = models.CharField(max_length=30)
get_project = models.IntegerField()
presonal_information = models.CharField(max_length=100)
tag = models.CharField(max_length=50)
accept_information = models.TextField()
class manager(models.Model):
username = models.CharField(max_length=30)
manage_project = models.IntegerField()
presonal_information = models.CharField(max_length=100)
tag = models.CharField(max_length=50)
accept_information = models.TextField()
class project(models.Model):
number = models.IntegerField()
project_publisher = models.CharField(max_length=30)
project_content = models.TextField()
project_receiver = models.CharField(max_length=30)
project_manager = models.CharField(max_length=30)
tag = models.CharField(max_length=50)
class UserProfile(models.Model):
#user = models.ForeignKey(User, unique=True, verbose_name='profile')
user = models.OneToOneField(User, unique=True)
identity = models.CharField(max_length=15)
admin.site.register(publisher)
admin.site.register(receiver)
admin.site.register(project)
admin.site.register(UserProfile)
3.django无法使用login
遇到了一个登录失败的问题。
一个初始版本的login如下:
def login(request):
if request.method == "POST":
print request.POST['login_username'],request.POST['login_password']
user = django.contrib.auth.authenticate(username=request.POST['login_username'],
password=request.POST['login_password'])
if user is not None:
django.contrib.auth.login(request, user)
return HttpResponseRedirect('/hello')
else:
return HttpResponse('fail to login')
return render(request, 'login.html', {})
我发现在判断user是不是存在的时候就一直判断不存在,反复检查登录的代码,虽然初级,但是应该是正确的。最后发现问题存在于注册上
注册时,我们的代码如下
def register(request):
if request.method == 'POST':
reg = User()
reg.username = request.POST['register_username']
reg.password = request.POST['register_password'] #问题出在这一行
reg.save()
pro = UserProfile(user_id=reg.id)
pro.identity = request.POST['register_identity']
pro.save()
return HttpResponse("run!")
return render(request, 'register.html', {})
事实上问题出在密码的设置上,我们应该使用django内置的set_password
方法,即可解决问题。
reg.set_password(request.POST['register_username'])
4.在django的admin后台看到的各个表的值都是乱码
出现问题的原因是在model.py内的类没有加__unicode__()
方法,导致admin不知道拿哪一个字段来显示。改进的方法类似下面的代码
from __future__ import unicode_literals
from django.contrib import admin
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class publisher(models.Model):
username = models.CharField(max_length=30)
order_project = models.IntegerField()
presonal_information = models.CharField(max_length=100)
accept_information = models.TextField()
#下面这个方法加上
def __unicode__(self):
return self.username
5.无法提交HTML表单
解决方法:在HTML表单内加上{% csrf_token %}
<form method="post">{% csrf_token %}
<table>
后面省略
6.(1054, "Unknown column 'coder_market_project.status' in 'field list'")
问题可能是对数据库做了修改之后只进行了python manage.py makemigrations
没有做python manage.py migrate
我想问题可能是在django 的数据库抽象层做了改动之后,但是连接的数据库并没有改变表或者是字段,在增删改查的时候django在对应的数据库里找不到相应的数据
7.django数据库查询时不确定是否存在
可以用类似下面的方法。核心是DoesNotExist
异常
try:
p = User.objects.get(username='haha')
except User.DoesNotExist:
return HttpResponseRedirect('/hello')
新博客迁移至https://chuxiuhong.com/