Django中一些配置

Posted luguochang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django中一些配置相关的知识,希望对你有一定的参考价值。

//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中一些配置的主要内容,如果未能解决你的问题,请参考以下文章

VS Code配置markdown代码片段

Django REST框架--认证和权限

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

[TimLinux] Django 信号

使用 Django 模板作为片段