码迷,mamicode.com
首页 > 其他好文 > 详细

Django中一些配置

时间:2020-01-11 13:33:25      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:ken   rac   dir   url   frame   数据库   rest   document   dsc   

//url.py
import xadmin
from MxShop.settings import MEDIA_ROOT
from django.views.static import serve
from rest_framework.documentation import include_docs_urls

url(r^xadmin/,xadmin.site.urls),
url(r^media/(?P<path>.*)$,serve,{"document_root":MEDIA_ROOT}),
url(rdocs/, include_docs_urls(title="标题")),

//settings.py

import sys
sys.path.insert(0,BASE_DIR)
sys.path.insert(0,os.path.join(BASE_DIR,apps))
sys.path.insert(0,os.path.join(BASE_DIR,extra_apps))

AUTH_USER_MODEL=user.UserProfile //重写用户表 , 去找这个位置

APP中添加
    user.apps.UserConfig,
    trade.apps.TradeConfig,
    goods.apps.GoodsConfig,
    user_operation.apps.UserOperationConfig,
    DjangoUeditor,
    crispy_forms,
    django_filters,
    xadmin,
    rest_framework,


//数据库

DATABASES = {
    default: {
        ENGINE: django.db.backends.mysql,
        NAME: "databaseName",
        USER: ROOT,
        PASSWORD: "xxxx",
        HOST: "xxx",
        OPTIONS: { init_command: SET default_storage_engine=INNODB; }
    }
}

//admin配置中文

LANGUAGE_CODE = zh-hans

TIME_ZONE = Asia/Shanghai

USE_I18N = True

USE_L10N = True

USE_TZ = False

//配置静态文件
LANGUAGE_CODE = zh-hans

TIME_ZONE = Asia/Shanghai

USE_I18N = True

USE_L10N = True

USE_TZ = False


models.py

from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
    """
    用户
    """
    name = models.CharField(max_length=30, null=True, blank=True, verbose_name="姓名")

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username  //写name会找不到,还是要写username

在别的models.py下要关联用户时

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

from goods.models import Goods
User = get_user_model()

class ShoppingCart(models.Model):
    """
    购物车
    """
    user = models.ForeignKey(User, verbose_name="用户",on_delete=models.CASCADE)
  
  class Meta:
  verbose_name = ‘购物车‘
  verbose_name_plural = verbose_name
  unique_together = ("user", "goods")

  def __str__(self):
  return "%s(%d)".format(self.goods.name, self.nums)
   

Django中一些配置

标签:ken   rac   dir   url   frame   数据库   rest   document   dsc   

原文地址:https://www.cnblogs.com/luguochang/p/12179337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!