I am learning django from a book. According to instruction I created a forms.py file inside the same directory that views.py exists. Inside the file I wrote the following codes:
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField()
email = forms.EmailField(required = False)
message = forms.CharField()
Then after entering virtual environment through env_mysite\scripts\activate I started python. After than I typed:
from mysite.forms import ContactForm
f = ContactForm()
But I receive the following message after a lot of file paths
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but
settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing
settings.
I opened setting.py file and USE_I18N was True
I have just followed the instruction from tutorial book I but don't understand what is wrong here. Could you please help
It seems like there's nothing wrong with that part of your project (the Form you've created there) or that language code configuration. It looks like you are simply not indicating which Django project to run, in your shell.
Please refer to this answer.
Command:
Run this command: python manage.py shell
Through this, you can access your models and form
Related
I am using a DRF serializer in my project as below
# my_serrializers.py
from rest_framework import serializers
class TssUpdateExtraVarsSerializer(serializers.Serializer):
cluster_name = serializers.CharField(required=True)
class TssUpdateSerializer(serializers.Serializer):
extra_vars = TssUpdateExtraVarsSerializer(required=True)
It works fine when I send a request and it handles the job.
But if I open a python console (the one in my project's venv) as below:
#python console
from my_serializers import TssUpdateSerializer
s = TssUpdateExtraVarsSerializer(data={"a": 1})
s.is_valid()
I get this error
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
what is the reason for it and how to fix it.
I want to be able to play with it outside of django. is it possible? how?
So i've got django-postman installed in my project. I've got the app installed and the urls are added but it's still not working.
When I try and send a message to the user test for example it's saying "Some usernames are unknown or no more active: test." which makes me think it's trying to use the wrong user model because the username exists in the database, it just can't find it.
I've got these in my settings if it helps.
POSTMAN_DISALLOW_ANONYMOUS = False
POSTMAN_DISALLOW_MULTIRECIPIENTS = False
AUTH_USER_MODEL = 'accounts.User'
Looking at the code for django-postman I have found out the issue. In postman.future_1_5.py it just imports User instead of what I need.
How can I change this code? Is there a way I can keep a file within my application and use that instead?
I'm thinking this will fix my issue:
from __future__ import unicode_literals
from accounts.models import MyUser
MyUser.USERNAME_FIELD = 'username'
MyUser.get_username = lambda self: self.username
def get_user_model():
return MyUser
Modifying future_1_5.py is not the solution to your issue, and is a very bad idea anyway. As its name implies, this file is intended to make the code runnable with Django before version 1.5.
I suppose you're running at least Django 1.5, so if this fallback is fired it means that your custom user model could not be imported.
The right explanation is that your setting is wrong. It has to name the effective model:
AUTH_USER_MODEL = 'accounts.MyUser'
i have installed photologue - A customizable plug-in photo management application for the Django web framework here into my project without problem...
now i want to change app name in admin page which is photologue. for this i have used ugettext_lazy but i got an error when i define this to all Meta:
from django.utils.translation import ugettext_lazy as _
class Meta:
app_label = _('newappname')
Error:
ValueError at /admin/
Cannot create form field for 'effect' yet, because its related model
'PhotoEffect' has not been loaded yet
Is there any easy way for changing app name, i have looked a lot but didnt find...
Django does not support app renaming in the admin right now, but ticket #3591 was raised to add that functionality, so hopefully it will be added.
There are several ways of acomplishing that. The simplest and preferred would be changing the main admin template and using whatever name you want as the app name there.
Other solutions:
How to change the name of a Django app?
https://stackoverflow.com/a/6312798/342473
http://djangosnippets.org/snippets/1882/
I know same question being asked many times. I have successfully setup all things. I didn't get this error when i run this on localhost. But when i run the site on deployment server i got this error not always but very often. When i refresh page 3 or 4 times the error disappears but keeps on coming again and again which is very annoying.
My directory structure related to problem is as:
project (folder)
profiles (folder)
models.py (file)
settings.py (file)
I have folder profilesin which there is models.py and i have class Profile there.
In settings.py i have included profiles folder to my apps also and i have set AUTH_PROFILE_MODULE = 'profiles.Profile'.
Also in Profile class i have added the path for application:
class Profile(UserenaLanguageBaseProfile):
class Meta:
app_label = 'profiles'
Can any one help me to figure out this issue??
AUTH_PROFILE_MODULE should be a string:
AUTH_PROFILE_MODULE = 'profiles.Profile'
I was using Django 1.4 trunck version, after upgrading the package again that problem is fixed.
Regarding Django Sites module and manage.py syncdb
The Auth module can prompt to ask for default superuser for the admin site, during .\manage.py syncdb. I would like to see similar things happen for the default site domain name. Currently it is example.com, hardcoded unless I use admin web site to change it. I want to change it during syncdb.
I made a small django app that can be plugged in and play. To plug it in:
download it into project directory or into where your project can find.
add, in your settings.py INSTALLED_APPS, "site_default" (the app name) at the end or after "django.contrib.sites" that it depends on.
Run manage.py syncdb
or manage.py createdefaultsite
Screen shot:
(pinax-dev)>manage.py createdefaultsite
Site domain name: mydomain.com
Site display name: My Site!
(pinax-dev)
It comes with a unit test.
To run unit test:
(pinax-dev)>manage.py test site_default
"site_default" is the app name.
Source code:
http://github.com/peiwei/pinax/raw/master/pinax/apps/site_default.tgz
More Screenshot:
(pinax-dev)> manage.py syncdb
Creating table...
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): yes
Username: administrator
E-mail address: who#who.com
Password:
Password (again):
Superuser created successfully.
Would you like to change the default site domain name? (yes/no)[default:no]: yes
Site domain name: mydomain.com
Site display name: My Site!
...
Installing index for signup_codes.SignupCode model
Installing index for signup_codes.SignupCodeResult model
You can do this yourself:
Create a management command to prompt for your new site
connect it to the post_syncdb signal
The command will let you set the site conveniently from the command line. Connecting it to the signal will mean you get prompted whenever the sites app is installed. eg:
from django.contrib.sites import models as sites_app
signals.post_syncdb.connect(create_site, sender=sites_app)
When writing the create_site function (signal handler), you can copy the auth module's approach almost exactly:
def create_site(app, created_models, verbosity, **kwargs):
from django.contrib.sites.models import Site
from django.core.management import call_command
if Site in created_models and kwargs.get('interactive', True):
msg = "\nYou just installed Django's sites system, which means you don't have " \
"any sites defined.\nWould you like to create one now? (yes/no): "
confirm = raw_input(msg)
while 1:
if confirm not in ('yes', 'no'):
confirm = raw_input('Please enter either "yes" or "no": ')
continue
if confirm == 'yes':
call_command("createsite", interactive=True)
break
Now you just need to create your management command createsite and you're done. I do wonder why this isn't already in Django though, I hate example.com.
Put all this into a little app and reuse it for every project your do. Bonus points if you post the app somewhere like google code or django's bug tracker.