I get this error when I add a something to the text field.I'm trying to create a project where someone can enter random messages.
OperationalError at /admin/submit/submit/add/
no such table: main.auth_user__old
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/submit/submit/add/
Django Version: 2.1
Exception Type: OperationalError
Exception Value:
no such table: main.auth_user__old
Exception Location: D:\Anaconda1\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 296
Python Executable: D:\Anaconda1\python.exe
Python Version: 3.8.3
Python Path:
['D:\\PyProjects\\Website',
'D:\\Anaconda1\\python38.zip',
'D:\\Anaconda1\\DLLs',
'D:\\Anaconda1\\lib',
'D:\\Anaconda1',
'C:\\Users\\heman\\AppData\\Roaming\\Python\\Python38\\site-packages',
'D:\\Anaconda1\\lib\\site-packages',
'D:\\Anaconda1\\lib\\site-packages\\win32',
'D:\\Anaconda1\\lib\\site-packages\\win32\\lib',
'D:\\Anaconda1\\lib\\site-packages\\Pythonwin']
Server time: Mon, 5 Oct 2020 17:19:26 +000
This is my models.py
from django.db import models
# Create your models here.
class Submit(models.Model):
title = models.TextField(max_length=140, default='SOME STRING')
status = models.TextField(max_length=140, default='SOME STRING')
description = models.TextField(max_length=140, default='SOME STRING')
Here is my setting.py installed apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'submit.apps.SubmitConfig',
]
Here is my apps.py
from django.apps import AppConfig
class SubmitConfig(AppConfig):
name = 'submit'
Here is my admin.py
from django.contrib import admin
from .models import Submit
# Register your models here.
admin.site.register(Submit)
I also ran python manage.py makemigrations
and python manage.py migrate
Please help me...Thanks in advance
This seems to work for me..
the original answer:
https://stackoverflow.com/a/59349457/13732795
But I'll copy paste it here too
Go to the virtual environment and install django#2.1.7
pip install django==2.1.7
Delete the db.sqlite3 file in your root folder.
Create the new db.sqlite3 in your root folder.
Re-run migrations:
python manage.py makemigrations
python manage.py migrate
Related
i want to activate my models in my files, my code in terminal:
(env) PS C:\Users\MadYar\Desktop\code.py\learning_log> python manage.py makemigrations learning_logs
error:
No changes detected in app 'learning_logs'
im using django to make a web application and i add models in my settings.py like that:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'learning_logs'
]
i dont know why it sais no changes detected in app 'learning_logs'
add empty __init__.py in learning_logs folder.
check if you have any models.py in learning_logs folder.
add some models, which inherited from django.models.Model:
from django import models
class MyModel(models.Model):
myfield = models.Charfield(max_length=255)
call python manage.py makemigrations learning_logs
if you see No changes detected in app 'learning_logs' again, try to read the tutorial, especially this part: https://docs.djangoproject.com/en/4.0/intro/tutorial02/
i'm a newbie to Django and i just started this project....
After Creating Superuser using this command python manage.py createsuperuser I tried to access the admin route but on typing the /admin on the browser it's displays this error on the browser
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/login
Using the URLconf defined in myblog.urls, Django tried these URL patterns, in this order:
1. admin/
I tried adding: LOGIN_REDIRECT_URL = '/admin/' in the settings.py as someone suggested on stackoverflow but it shows the same thing.
Kindly direct me on what i'm not doing right
You need to run:
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Check if you have on settings:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
Your apps
]
And in your URLs from the app:
urlpatterns = [
#
path('admin/', admin.site.urls)
Your apps urls
]
I finally got it right
I started the project from beginning but i made slight changes I did the following:
Created a new folder for my project
cd into the folder
Run python -m venv name-of-virtual-env
cd into name-of-virtual-env
cd into Scripts
Run activate.bat
cd ../../
The name of the virtual environment will appear inside bracket near your directory name, indicating that you've activated your Virtualenv.
Then you start your project.... using
django-admin startproject (name of the project)
I am trying to query data from database using Python shell. settings.py includes:
import django
django.setup()
...
INSTALLED_APPS = [
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products.apps.ProductsConfig',
'users.apps.UsersConfig',
'crispy_forms',
]
When i open Python shell i do:
> from django.conf import settings
> settings.configure()
Then I try to import models:
> from products.models import Product
However, Python returns:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I tried adding django.setup() call in settings and also moving this statement after INSTALLED_APPS.
EDIT: With django.setup() I get the following error when I try to run command runserver:
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
As you noticed, django hasn't been properly initialized and so you are getting this message.
As #davit-tovmasyan mentioned, there is a built in manage.py command to open a django shell in the correct context:
./manage.py shell
Additionally, if you install django-extensions there is a very helpful command which imports all your models, plus common imports:
$ ./manage.py shell_plus
# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from project.my_app.models import Model1, Model2
# ...etc, for all django and project apps
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
# ...
>>> type your python here
If you want to run your own script, for example in temp.py, then you can copy the manage.py code into a new file and run it directly:
import os
import django
# these must be before any other imports of django app code/models
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
from my_app.models import Product
print(Product.objects.all())
# at the command line:
$> chmod +x temp.py
$> ./tmp.py
Also, with django-extensions run_script, is the scripts folder where you can add simple python scripts with a run() method.
I have models.py in a Django app that has a few models that I have the corresponding tables for in MySQL, and others that I do not.
How do I get Django to create the tables for the models that don't have them?
I tried makemigrations and migrate, but they were not delivered.
But then again, when running tests under vcc_merged/cards_browser django.db.utils.ProgrammingError: (1146, "Table 'test_vcc.polls_choice' doesn't exist")
What am I doing wrong?
I am using Django 1.11.7, Python 3.6.3 on MySQL 5.7
apps.py for browser app:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
class BrowserConfig(AppConfig):
name = 'browser'
INSTALLED_APPS = [
'vcc_merged',
'vcc_merged.cards_browser',
'browser',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Try with
Python manage.py makemigrations
Than
Python manage.py migrate
It will work for you.
I'm following a django blog tutorial. When I used python manage.py shell and tried following commands in shell
from mainsite.models import Category, Tag
c = Category(name='category test')
c.save()
this error occured:
sqlite3.OperationalError: no such table: mainsite_category
This is my model.py in mainsite folder, which I think is correct:
from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
I have tried manage.py makemigrations, manage.py migrate and can successfully use manage.py runserver to see the website. So what is wrong here?
Edit: this is my INSTALLED APP in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
]
And my project structure
|____djangoBlog
| |______init__.py
| |____settings.py
| |____urls.py
| |____wsgi.py
|____mainsite
| |______init__.py
| |____admin.py
| |____apps.py
| |____models.py
| |____tests.py
| |____urls.py
| |____views.py
|____manage.py
|____README.md
|____requirements.txt
|____templates
| |____blog
| | |____index.html
Edit: When I run python manage.py makemigrations and python manage.py migrate, the output is as follows:
No changes detected
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
UPD:
Since your project structure is lack of migrations folder in mainsite app, it means that you haven't created migrations for that app. Run python manage.py makemigrations mainsite and then python manage.py migrate.
From docs:
To add migrations to an app that doesn’t have a migrations directory, run makemigrations with the app’s app_label.
And yes, you can create model with simple instance creation. From docs Creating objects.
Original answer:
You need to be sure that you've included your 'mainsite' app in INSTALLED_APPS in settings.py and then run migrations. After that you should be able to do the following:
c = Category.objects.create(name='category test')
c.save()
#Owen,
According to vishes_shell's answer, you'll have to do :
python manage.py makemigrations mainsite
the first time in order to generate a folder migrations that will contain your migrations, then you can just do
python manage.py makemigrations
In the futur if you add a new app, don't forget to make again the first command (changing the "mainsite" by the name of your app) before using the second one.
It seems like in your models.py you need an indentation at name:
class Category(models.Model):
name = models.CharField(max_length=100)