Django Registration Redux Registration form setting - python

In Django Registration Redux, there are alternative registration forms that one can use. For Example, one can use RegistrationFormTermsOfService or RegistrationFormUniqueEmail in the registration.forms file. I read the code and figure that the way to do this is by setting a REGISTRATION_FORM variable in the settings.py file. In registration.views we see the following:
REGISTRATION_FORM_PATH = getattr(settings, 'REGISTRATION_FORM','registration.forms.RegistrationForm')
REGISTRATION_FORM = import_string( REGISTRATION_FORM_PATH )
However, when I do set the REGISTRATION_FORM in settings.py to 'registration.forms.RegistrationFormUniqueEmail', I still can't get the form for unique email. Any help will be appreciated.

Okay, so I figured this one out. Basically, in the version 1.1 of django registration redux, this option of setting which registration form in the setting was not available. The code I was looking at from github is the version 1.2 version. So if this happens to you, just install from github.

Related

save() doesn't work in Mongoengine

I'm trying to perform a simple insert operation using Mongoengine and Django.
Regarding my project structure simply I have a project, AProject and an app, AnApp. I have a running mongo in a remote machine with an IP of X.X.X.X. I am able to insert document using Robomongo in it.
I have removed the default Database configuration part of the settings.py located inside the AProject directory. The newly added lines are shown below:
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
import mongoengine
# ----------- MongoDB stuff
from mongoengine import register_connection
register_connection(alias='default', name='AProject', host='X.X.X.X')
# ----------
Now let me show the models.py and the views.py located inside AnApp.
models.py
from mongoengine import Document, StringField
class Confession(Document):
confession = StringField(required=True)
views.py
from django.http import HttpResponse
from models import Confession
from mongoengine import connect
def index(request):
connect('HacettepeItiraf', alias='default')
confession = Confession()
confession.confession = 'First confession from the API'
print(confession.confession + ' Printable') # The output is --First confession from the API Printable--
print(confession.save()) # The output is --Confession object--
return HttpResponse(request)
The urls.py located inside AProject is simply as below:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^confessions/', include('confession.urls')),
url(r'^admin/', admin.site.urls),
]
When I enter http://127.0.0.1:10000/confessions/ I see a blank screen which I expect. However there is nothing saved from the API. I get the expected output except Confession object.
How can I solve this problem?
EDIT:
I found the concrete proof that currently MongoEngine's support for Django is unstable and it is corresponding to Django version 1.9:
MongoEngine documentation on Django support states:
Django support has been split from the main MongoEngine repository. The legacy Django extension may be found bundled with the 0.9 release of MongoEngine.
and
6.1. Help Wanted!
The MongoEngine team is looking for help contributing and maintaining a new Django extension for MongoEngine! If you have Django experience and would like to help contribute to the project, please get in touch on the mailing list or by simply contributing on GitHub.
Which leads to this repo, where the following is stated:
THIS IS UNSTABLE PROJECT, IF YOU WANT TO USE IT - FIX WHAT YOU NEED
Right now we're targeting to get things working on Django 1.9
So it may be possible that it cannot play well with Django at this state or with your version and thus the problem occurs.
Initial attempt, leaving it here for legacy reasons.
I believe that the problem occurs on how you are initializing your object, although I do not have a set up to test this theory.
It is generally considered that is better to make a new object with the .create() method:
def index(request):
connect('HacettepeItiraf', alias='default')
confession = Confession.objects.create(
confession='First confession from the API'
)
print(confession.confession + ' Printable')
confession.save()
return HttpResponse(request)
Have a look at the Django & MongoDB tutorial for more details.
In this tutorial, the supported Django version is not mentioned, but I haven't found concrete proof that MongoDB Engine can or can't play well with Django version > 1.8.
Good luck :)

Zinnia has no content field in Django Admin

I have setup Django CMS with Zinnia for the first time. However in the admin area there is no place to enter actual content! See image...
Everything else works. What I'm I doing wrong here?
The problem is that you're using this plugin https://github.com/django-blog-zinnia/cmsplugin-zinnia which reset admin page here if you're set cmsplugin_zinnia.placeholder.EntryPlaceholder as ENTRY_BASE_MODEL.
As you see this code cuts the original fieldset and remove content field:
fieldsets = (
(_('Content'), {'fields': (('title', 'status'), 'image')}),) + \
EntryAdmin.fieldsets[1:]
So I see only one solution is to set another model as ENTRY_BASE_MODEL which you need to create:
from zinnia.models_bases.entry import AbstractEntry
class Entry(AbstractEntry):
pass
And finally set correct settings.py
ENTRY_BASE_MODEL = 'path_to_module.Entry'
I hope this will help you :)
Note
I digged out why the hell they reset fieldset the original admin. This is explained here.

Django registration-profile - next step to a user profile

I have installed the django-registration app to my project. After a successful log in step, I am redirecting the user to localhost:8000/ - this is my default testing host and port. And I am displaying somewhere on the page, the username of the logged in user.
What I want to do now is that when I click the username some options like edit profile or change password will appear. My questions are the following:
Should I create another model (inside another new app) containing fields like profile photo, gender, birthday etc and add a foreign key to the User model from django.contrib.auth.models ? Or should I modify the model from django-registration to add some additional fields but which I do not ask for at registration phase and only update them later?
if I want my profile edit feature to be at /accounts/edit, which would be the best practice to do it? to edit the URLconf of my project and add a line like (r'^accounts/edit$',.....) just before (r'^accounts/', include('registration.backends.default.urls')), ?
I hope I made myself clear. I'm trying to figure out which would be the best approach before coding, as I am new to Django... Thanks
I find it's easier to decouple the profile table from the auth table. Just like you mentioned you can use a foreign key relationship to link that profile to the user. You can also apply a lambda inside of your profile table to automatically create a profile when a new user object is created.
Inside your template you can link to the profile page dynamically based on the current authenticated party by using
{% if request.user.is_authenticated %}
Update Profile
{% endif %}
user_profile being the name of your app which holds your user_profile table. That way when the request is made you use the regular expression for the current user id (similar to the polls example provided by django) to get the id number of the currently logged in user than inside the views you just query the database for that particular user.
views.py
def myView(request, user_id):
userProfile = UserProfile.objects.get(user.pk=user_id)
This is a high level example to give an idea of one way to accomplish it.

How to allow editing of templates for email by admin user in Django?

I need to allow the admin user to make an email template in html, and the user should be able to add dynamic variables where needed. The template will be saved in the database. For example,
Dear {{user.first_name}},
Thanks for participating in our cooking class on {{cooking_class.date}}
Then, there will be a cron job which will send emails and fill the dynamic variables.
What are my options? Is there a django package for this? I am using Django 1.4.3
thanks
I implemented something similar, here's what I did:
Create an app, and registered a model to store the email
Set up a management command to process the email
Manage the cron job via django-chronograph
Rendering the email, I used render_to_string, for example
from django.template.loader import render_to_string
from myproject.myemailapp.models import EmailTpl
email_tpl = EmailTpl.objects.get(...#criteria here)
# fetch the rest of your dynamic variables
rendered_tpl = render_to_string(email_tpl.user_entered_tpl, {
"user": user,
"cooking_class": cooking_class,
# ... and so on
})
Alternatively, Django Packages has some packages you might want to look into. Would be great if you post back the route you decided.
The plugin django-dbtemplates allows you to store templates in your database, and you can expose the dbtemplate app on your Admin site to edit. You can set your middleware settings such that either the database template or the same template in other locations has priority for loading.
We are using this in a project to manage templates for different products, with the database-stored template for each product linked by ForeignKey.

Password protect a whole django app

I am running a simple staging env on heroku and I am now looking to password protect the whole app with some sort of simple authentication
I am wondering if there is a simple app or middleware that already supports this.
Have tried looking around for solutions with Heroku / Cloudflare and django, but nothing seems really straight forward.
Django 1.3.1
I use django-lockdown for exactly this purpose. It allows you to add a simple password over the whole of a dev site, without having to add in any extra auth bits on your views that aren't used outside of a dev environment. It also means you can login as admin, or regular users to test whatever your site does
https://github.com/Dunedan/django-lockdown
I use Heroku and Lockdown with this bit of code in my settings.py file
USE_LOCKDOWN = os.environ.get('USE_LOCKDOWN', 'False') == 'True'
if USE_LOCKDOWN:
INSTALLED_APPS += ('lockdown',)
MIDDLEWARE_CLASSES += ('lockdown.middleware.LockdownMiddleware',)
LOCKDOWN_PASSWORDS = (os.environ.get('LOCKDOWN_PASSWORD', 'False'),)
LOCKDOWN_URL_EXCEPTIONS = (r'^/some/url/not/locked/down/$',)
Then obviously set a config var of USE_LOCKDOWN as True on my dev site, and False on my production site so no need to change the code for either.
Django's authentication framework has built-in utilities #login_required which helps you to password protect your view functions (and its corresponding "url" of course).
Usage like this:-
from django.contrib.auth.decorators import permission_required, login_required
#login_required
def view_organization(request, org_slug):
"""
Only permit a logged in user to view the organization.
"""
org = get_object_or_404(organization, slug=org_slug)
org_users = organizationuser.objects.filter(organization=org,\
organization__is_active=true)
template = 'organizations/view_organization.html'
template_vars = {'org_users': org_users, 'org': org}
return render(request, template, template_vars)
For advanced access control, use #permission_required decorator.

Categories