小编Raj*_*pta的帖子

Django 完整性错误 1452,无法添加或更新子行:外键约束失败

模型.py:

from django.db import models

class Line(models.Model):
  text = models.CharField(primary_key=True, max_length=255)

class Line2 (models.Model):
  text_line = models.ForeignKey ("Line", primary_key = True)
Run Code Online (Sandbox Code Playgroud)

视图.py:

from django.core.context_processors import csrf
from django.shortcuts import render
import datetime
from models import Line, Line2

def test_page (request):
  if 'uid' in request.POST:
    user_id = request.POST['uid']
    line_query = Line.objects.get (text = user_id)
    new_entry = Line2 (text_line_id = line_query)
    new_entry.save()
    return render(request, "templateFiles/testPage1.html", {"page_result":"Data Entered"});
  else:
    return render(request, "templateFiles/testPage1.html")
Run Code Online (Sandbox Code Playgroud)

模板:

<div id = "header">
  {{ page_result }}
  <form action="http://127.0.0.1:8000/test_page/" method …
Run Code Online (Sandbox Code Playgroud)

python mysql django

5
推荐指数
1
解决办法
1万
查看次数

Django mysql数据库凭据read_default_file提供错误

我正在从Django项目的另一个conf文件中提供mySql凭据。之前它可以正常工作,但是现在在我重新安装Ubuntu后它停止了工作。

settings.py

 DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.mysql',
         'OPTIONS': {
           'read_default_file': './connection.cnf',
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

connection.cnf

 [client]
 database=test
 user=root
 password=rajiv@123
 HOST=localhost
 PORT=3306
 default-charset=utf8
Run Code Online (Sandbox Code Playgroud)

另外,conf文件的位置如下:

 rajiv@ubuntu:~/djangoProjects/test$ ls -l
 total 9
 drwxrwxrwx 1 root root 4096 Dec 28 11:04 test
 drwxrwxrwx 1 root root 4096 Dec 27 14:00 testApp
 -rwxrwxrwx 1 root root  103 Dec  3 00:37 connection.cnf
 -rwxrwxrwx 1 root root  252 Oct 15 11:36 manage.py
Run Code Online (Sandbox Code Playgroud)

给出的错误如下:

 rajiv@ubuntu:~/djangoProjects/test$ python manage.py runserver
 Performing system checks...

 Warning: World-writable config file …
Run Code Online (Sandbox Code Playgroud)

python mysql django

1
推荐指数
1
解决办法
2306
查看次数

标签 统计

django ×2

mysql ×2

python ×2