首页 > 解决方案 > 模型表与用户的连接(标准登录模块)。姜戈,蟒蛇

问题描述

我不知道这篇文章是否有一个好标题,但我正在解释我的意思。

我在 django 中有一个标准登录模块,我希望我的用户只显示他们自己的 cookie。如何在我的模型中连接用户(厨师)?

class Cookies(models.Model):
    """My Cookies"""
    name = models.CharField(max_length=200)
    photo = models.FileField()
    next_information = models.ManyToManyField(Someone)
    'user = ?' <-- how to assign a cook as a user to Cookies

然后我想在views.py中过滤我的用户(厨师)(谁添加了自己的新cookie到表中),这样登录后只会显示给定厨师创建的cookie。

在 view.py 中使用

Cookies.objects.filter('user = ?')...

总而言之,如何将标准用户与数据库中的模型连接起来。我有 10 位厨师,每个人都有自己的蛋糕。在 views.py 中,只显示了厨师的 cookie。

非常感谢您的帮助。我很抱歉,因为我以一种难以理解的方式写了这篇文章。如果有什么不清楚(在我的问题中),我会尝试更好地解释它。

视图.py

def cookies_list(request):
    cookies = Cookies.objects.filter(user=request.user)
    context = { 'cookies':cookies }
    return render(request, 'account/templates.html', context)

模板.html

{% block content %}
{% for cookies in cookies_list  %}
<label for="file-1">
<span class="text-white">Hello world</span>
</label>
{% endfor %}
{% endblock %}

设置.py

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'media_app',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'app_rama.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'app_rama.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

# Miejsce szukania pliekow statycznych w django
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '/static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# konie. Kod szuka plikow statycznych w folderze glownym, a nie tylko w naszych aplikacjach.

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_app/media')

标签: pythondjango

解决方案


你可以使用外键关系

class Cookies(models.Model):
"""My Cookies"""
  name = models.CharField(max_length=200)
  photo = models.FileField()
  next_information = models.ManyToManyField(Someone)
  user = models.ForeignKey(
    settings.AUTH_USER_MODEL,
    on_delete=models.CASCADE,
)

在视图中,您可以过滤 cookie,

cookeis = Cookies.objects.filter(user=request.user)

推荐阅读