简体   繁体   English

Python Django - change_view() 缺少 1 个必需的位置参数:'object_id'

[英]Python Django - change_view() missing 1 required positional argument: 'object_id'

I am new in Django Framework and currently working on first project.我是 Django 框架的新手,目前正在从事第一个项目。 I made a simple contact form it takes data from users and save it into database.我制作了一个简单的联系表格,它从用户那里获取数据并将其保存到数据库中。 Everything is working right.一切正常。 But when I login into my admin panel and go into the Contacts and click on the data that I received.但是当我登录我的管理面板和 go 进入联系人并单击我收到的数据时。 I am getting error "change_view() missing 1 required positional argument: 'object_id'".我收到错误“change_view() 缺少 1 个必需的位置参数:'object_id'”。

my admin.py我的admin.py

from home.views import contact
from django.contrib import admin
from home.models import Contact

# Register your models here.
admin.site.register(Contact)

my models.py我的models.py

from django.db import models

# Create your models here.
class Contact(models.Model):
    name = models.CharField(max_length=30)
    email = models.EmailField()
    phone = models.CharField(max_length=10)
    desc = models.TextField()

my views.py我的views.py

from home.models import Contact
from django.shortcuts import render, HttpResponse
from home.models import models

# Create your views here.
def home(request):
    return render(request, 'home.html')

def about(request):
    return render(request, 'about.html')
    

def projects(request):
    return render(request, 'projects.html')

def contact(request):
    if request.method=='POST':
        print('This is POST')
        name = request.POST['name']
        email = request.POST['email']
        phone = request.POST['phone']
        desc = request.POST['desc']
        # print(name, email, phone, desc)
        contact = Contact(name=name, email=email, phone=phone, desc=desc)
        contact.save()
        print('DAta has been written into the Database')
    # return HttpResponse("This is My Contact")
    return render(request, 'contact.html')

my settings.py我的settings.py

"""
Django settings for pwhTutorials project.

Generated by 'django-admin startproject' using Django 3.1.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-snip-'

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

ALLOWED_HOSTS = []


# Application definition

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

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 = 'pwhTutorials.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['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 = 'pwhTutorials.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.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/3.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/3.1/howto/static-files/

STATIC_URL = '/static/'

my urls.py我的urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name = 'home'),
    path('about/', views.about, name = 'about'),
    path('projects/', views.projects, name = 'projects'),
    path('contact/', views.contact, name = 'contact'),
    
]

Project's urls.py项目的urls.py

"""
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("home.urls"))


]

I am new in Django Framework and currently working on first project.我是 Django 框架的新手,目前正在从事第一个项目。 I made a simple contact form it takes data from users and save it into database.我制作了一个简单的联系表格,它从用户那里获取数据并将其保存到数据库中。 Everything is working right.一切正常。 But when I login into my admin panel and go into the Contacts and click on the data that I received.但是当我登录我的管理面板和 go 进入联系人并单击我收到的数据时。 I am getting error "change_view() missing 1 required positional argument: 'object_id'".我收到错误“change_view() 缺少 1 个必需的位置参数:'object_id'”。

This is the image link https://ibb.co/LScd2Mb这是图片链接https://ibb.co/LScd2Mb

You missed """ at the top of your projects urls.py. Please correct, hope this will solve your problem.您错过了项目 urls.py 顶部的“”。请更正,希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM