django 创建model(数据库表)失败

在models.py中创建数据库表
 
 
from django.db import models

# Create your models here.
class Book(models.Model):
    name = models.CharField(max_length=20)
    price = models.IntegerField
    pub_date = models.DateField
 
 
 
在terminal中 :python manage.py makemigrations
 
返回结果:No changes detected
创建失败。
 
原因可能如下:
  1. migration folder You need a migrations package in your app.
  2. INSTALLED_APPS You need your app to be specified in the INSTALLED_APPS .dict
  3. Verbosity start by running makemigrations -v 3 for verbosity. This might shed some light on the problem.
  4. Full path In INSTALLED_APPS it is recommended to specify the full module app config path 'apply.apps.MyAppConfig'
  5. --settings you might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings
  6. specify app name explicitly put the app name in manage.py makemigrations myapp- that narrows down the migrations for the app alone and helps you isolate the problem.
  7. model meta check you have the right app_label in your model meta
  8. Debug django debug django core script. makemigrations command is pretty much straight forward. Here's how to do it in pycharm. change your script definition accordingly (ex: makemigrations --traceback myapp)
 
而我的情况是第二种 :setting.py内的INSTALLED_APPS未添加 app名字
 
 
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     'app1',
]
View Code

 

 

 
添加上后,创建成功
 

 

转载于:https://www.cnblogs.com/huangguoming/p/9990358.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的购买水果项目的实例代码,使用了 Django 框架和 SQLite3 数据库。本项目涉及前后端交互和数据库操作。 首先,需要安装 Django 和 SQLite3: ``` pip install Django pip install pysqlite3 ``` 然后创建一个 Django 项目: ``` django-admin startproject fruitstore ``` 进入项目目录: ``` cd fruitstore ``` 创建一个应用: ``` python manage.py startapp shop ``` 接下来,我们需要创建数据库。在 `shop` 应用的 `models.py` 文件中定义数据模型: ```python from django.db import models class Fruit(models.Model): name = models.CharField(max_length=50) price = models.DecimalField(max_digits=6, decimal_places=2) image_url = models.URLField() class Order(models.Model): fruit = models.ForeignKey(Fruit, on_delete=models.CASCADE) quantity = models.PositiveIntegerField(default=1) total_price = models.DecimalField(max_digits=6, decimal_places=2) ``` 然后在 `settings.py` 中设置数据库配置: ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } ``` 执行数据库迁移: ``` python manage.py makemigrations python manage.py migrate ``` 接下来,我们需要创建视图函数和模板。在 `shop` 应用的 `views.py` 文件中定义视图函数: ```python from django.shortcuts import render from django.http import HttpResponseRedirect, JsonResponse from .models import Fruit, Order def index(request): fruits = Fruit.objects.all() context = {'fruits': fruits} return render(request, 'shop/index.html', context) def add_to_cart(request): fruit_id = request.POST.get('fruit_id') quantity = int(request.POST.get('quantity')) fruit = Fruit.objects.get(id=fruit_id) total_price = fruit.price * quantity order = Order(fruit=fruit, quantity=quantity, total_price=total_price) order.save() return JsonResponse({'success': True}) ``` 在 `shop` 应用下创建 `templates/shop/index.html` 模板文件,用于显示水果列和购物车单: ```html {% extends 'base.html' %} {% block content %} <h1>水果商店</h1> <ul> {% for fruit in fruits %} <li> {{ fruit.name }} - ¥{{ fruit.price }} <form class="add-to-cart-form" method="POST" action="{% url 'add_to_cart' %}"> {% csrf_token %} <input type="hidden" name="fruit_id" value="{{ fruit.id }}"> <input type="number" name="quantity" value="1" min="1"> <button type="submit">加入购物车</button> </form> </li> {% endfor %} </ul> {% endblock %} {% block scripts %} <script> $(document).ready(function() { $('.add-to-cart-form').on('submit', function(event) { event.preventDefault(); var form = $(this); var formData = form.serialize(); $.ajax({ url: form.attr('action'), type: 'POST', data: formData, success: function(response) { alert('加入购物车成功!'); }, error: function(jqXHR, status, error) { alert('加入购物车失败!'); } }); }); }); </script> {% endblock %} ``` 最后,在项目的 `urls.py` 文件中设置路由: ```python from django.urls import path from .views import index, add_to_cart urlpatterns = [ path('', index, name='index'), path('add-to-cart/', add_to_cart, name='add_to_cart'), ] ``` 现在,启动开发服务器: ``` python manage.py runserver ``` 访问 `http://127.0.0.1:8000/` 就可以看到水果商店页面了。添加水果到购物车后,购物车数据会保存在 SQLite3 数据库中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值