Django Postman Messaging - python

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'

Related

django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N

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

Change name of the "Authentication and Authorization" menu in Django/python

I'm learning python/Django and setting up my first project. All is going well but I've been searching like crazy on something very simple.
There is a default menu item "Authentication and Authorization" and I want to change the name. I've searched in the template if I need to extend something, I've searched if there's a .po file or what not but I can't find it nor a hint on which parameter I should overwrite in admin.py to set it.
I'm not trying to install multi language or some advanced localization, just want to change the name of that one menu item :)
Any ideas?
To follow up on Lorenzo's answer (Django 3.0):
Create a file with your custom AppConfig somewhere in your project
# 'authconfig.py'
from django.apps import AppConfig
class CustomAuthConfig(AppConfig):
name = 'django.contrib.auth'
verbose_name = 'my super special auth name'
Then, in settings.py:
INSTALLED_APPS = [
...
#'django.contrib.auth', # comment out to avoid duplicate apps
'authconfig.CustomAuthConfig', # add this in for custom config of auth module
...
]
Create a custom AppConfig pointing to original django.auth module and override verbose_name. Then use your custom AppConfig in INSTALLED_APPS instead of original auth app.
https://docs.djangoproject.com/en/1.10/ref/applications/#configuring-applications
This can't be done at least cleanly via templates..
You can put the auth app verbose name "authentication and authorization" in your own .po file (& follow Django docs on translation)
This way Django will normally use your name.
If you are using a custom locale (refer to How to override the django admin translation?) then the best way is to add these lines to the django.po file:
msgid "Authentication and Authorization"
msgstr "Your text for the auth app"
For some reason many localization files fail to include a translation for the "Authentication and Authorization" string. Adding it like that (and using the compilemessages command as mentioned in the link above) should work just fine.

auth is not available in module

I have a web2py application where I have written various modules which hold business logic and database related stuff. In one of the files I am trying to access auth.settings.table_user_name but it doesn't work and throws and error as global name 'auth' is not defined. If I write the same line in controller, it works. But I want it to be accessed in module file. Please suggest how do I do that.
In your model file, where you define auth:
from gluon import current
auth = Auth(db)
current.auth = auth
Then in a module:
from gluon import current
def some_function():
auth = current.auth
....
For more details, see http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules.
I was getting a very similar error ("name 'auth' is not defined"). Had to add from django.contrib import auth at the top of views.py and it worked.

Django changing app name of Photologue

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/

Getting a 'AttributeError at /login/authenticated', 'User' object has no attribute 'backend'

I'm using the Python-Oauth2 library found here, Django 1.3. I'll end up using the Twitter library located here.
I've searched everything I could on this, but I can't see why I'm getting this error. When someone else asked this question, they were told it was because they didn't call for an authenticate(), but I have that right above the login.
I've managed to figure out that the REASON it is failing is somehow drawn into the call to authenticate() with Django. I confirmed this by commenting out the login- which promptly gets rid of the error. Something about that authenticate, whether it's the profile/user information or something of that nature, is failing.
Here's the code from my views.py:
def twitter_authenticated(request):
# Step 1. Use the request token in the session to build a new client.
token = oauth.Token(request.session['request_token']['oauth_token'],
request.session['request_token']['oauth_token_secret'])
client = oauth.Client(consumer, token)
# Step 2. Request the authorized access token from Twitter.
resp, content = client.request(access_token_url, "GET")
if resp['status'] != '200':
print content
raise Exception("Invalid response from Twitter.")
"""
This is what you'll get back from Twitter. Note that it includes the
user's user_id and screen_name.
{
'oauth_token_secret': 'IcJXPiJh8be3BjDWW50uCY31chyhsMHEhqJVsphC3M',
'user_id': '120889797',
'oauth_token': '120889797-H5zNnM3qE0iFoTTpNEHIz3noL9FKzXiOxwtnyVOD',
'screen_name': 'heyismysiteup'
}
"""
access_token = dict(cgi.parse_qsl(content))
# Step 3. Lookup the user or create them if they don't exist.
try:
user = User.objects.get(username=access_token['screen_name'])
except User.DoesNotExist:
# When creating the user I just use their screen_name#twitter.com
# for their email and the oauth_token_secret for their password.
# These two things will likely never be used. Alternatively, you
# can prompt them for their email here. Either way, the password
# should never be used.
user = User.objects.create_user(access_token['screen_name'], '%s#twitter.com' % access_token['screen_name'], access_token['oauth_token_secret'])
# Save our permanent token and secret for later.
profile = Profile()
profile.user = user
profile.oauth_token = access_token['oauth_token']
profile.oauth_secret = access_token['oauth_token_secret']
profile.save()
# Authenticate the user and log them in using Django's pre-built
# functions for these things.
user = authenticate(username=access_token['screen_name'], password=access_token['oauth_token_secret'])
login(request, user)
return HttpResponseRedirect('/')
Actually, the issue was the authenticate call in my authenticate view function was failing due to the password (which is actually the access_token['oauth_token_secret']).
So that we're on the same page, I'm talking about the def twitter_authenticated(request): function, that is included in the python-oauth2 github page itself, in the code example, in the views code section.
Due to the fact that I upgraded everything else, as well, along with Django 1.3, I had the wrong password in the auth_user table.
To summarize: simply look at the Django debugging output when loading your page (I'm assuming that you have DEBUG = True in your settings) and then narrow it down to which exact call. I'm pretty sure the function that fails for you, is the one that failed for me, though, which is
user = authenticate(username='blahblah', password=access_token['oauth_token_secret'])
So make sure the auth_user table/password field is the correct SHA1 of your access_token['oauth_token_secret'].
The reason I had the wrong one... I got a new computer, so I was installing all of the latest of everything, but didn't carry over the database data from my old computer.
I've got the same issue. I just noticed that the github page for Python-Oauth2 has the following message that's related to the views.py file specifically:
"NOTE: The following code was coded for Python 2.4 so some of the libraries and code here might need to be updated if you are using Python 2.6+."
I think I'm going to try downgrading to 2.5 temporarily just to narrow down whether it's a django 1.3 thing, or python.

Categories

Resources