Updating your app’s tests to work with djangotenants¶ Because django will not create tenants for you during your tests, we have packed some custom test cases and other utilities. If you want a test to happen at any of the tenant’s domain, you can use the test case TenantTestCase. This runner has been added to Django 1.6 as the default test runner. If you use Django 1.6 or above you don't need this app. An alternative Django TESTRUNNER which uses the unittest2 test discovery from a base path specified in the settings, or any other module or package specified to the test management command - including app tests.
- Django-jenkins designed to. Work with the default test. Command and the discover. Nose plugin for running. Selenium tests with django. This is a custom test runner.
- Using a custom test runner to test with celery ¶. If you’re going the CELERYALWAYSEAGER route, which is probably better than just never testing some parts of your app, a custom Django test runner does the trick. Celery provides a simple test runner, but it’s easy enough to roll your own if you have other things that need to be done.
- Using a custom test runner to test with celery¶ If you’re going the CELERYALWAYSEAGER route, which is probably better than just never testing some parts of your app, a custom Django test runner does the trick. Celery provides a simple test runner, but it’s easy enough to roll your own if you have other things that need to be done.
Django ships with a built-in User model for authentication and if you'd like a basic tutorial on how to implement log in, log out, sign up and so on see the Django Login and Logout tutorial for more.
However, for a real-world project, the official Django documentationhighly recommends using a custom user model instead. This provides far more flexibility down the line so, as a general rule, always use a custom user model for all new Django projects.
But how to implement one? The official documentation example is not actually what many Django experts recommend using. There is a far easier yet still powerful approach to starting off new Django projects with a custom user model which I'll demonstrate here.
Setup
To start, create a new Django project from the command line. We need to do several things:
- create and navigate into a dedicated directory called
accounts
for our code - install Django
- make a new Django project called
config
- make a new app
accounts
- start the local web server
Here are the commands to run:
Note that we did not run
migrate
to configure our database. It's important to wait until after we've created our new custom user model before doing so.If you navigate to http://127.0.0.1:8000 you’ll see the Django welcome screen.
Sweet. For now, stop the local server with
Control+c
because otherwise it will start kicking off lots of errors as we implement a custom user model.AbstractUser vs AbstractBaseUser
There are two modern ways to create a custom user model in Django:
AbstractUser
and AbstractBaseUser
. In both cases we can subclass them to extend existing functionality however AbstractBaseUser
requires much, much more work. Seriously, don't mess with it unless you really know what you're doing. And if you did, you wouldn't be reading this tutorial, would you?So we'll use
AbstractUser
which actually subclasses AbstractBaseUser
but provides more default configuration.Custom User Model
Creating our initial custom user model requires four steps:
- update
config/settings.py
- create a new
CustomUser
model - create new
UserCreation
andUserChangeForm
- update the admin
In
settings.py
we'll add the accounts
app and use the AUTH_USER_MODEL
config to tell Django to use our new custom user model in place of the built-in User
model. We'll call our custom user model CustomUser
.Within
INSTALLED_APPS
add accounts
at the bottom. Then at the bottom of the entire file, add the AUTH_USER_MODEL
config.Now update
accounts/models.py
with a new User model which we'll call CustomUser
.We need new versions of two form methods that receive heavy use working with users. Stop the local server with
Control+c
and create a new file in the accounts
app called forms.py
.We'll update it with the following code to largely subclass the existing forms.
Finally we update
admin.py
since the Admin is highly coupled to the default User model.And we're done! We can now run
makemigrations
and migrate
for the first time to create a new database that uses the custom user model.Superuser
It's helpful to create a superuser that we can use to log in to the admin and test out log in/log out. On the command line type the following command and go through the prompts.
Templates/Views/URLs
Our goal is a homepage with links to log in, log out, and sign up. Start by updating
settings.py
to use a project-level templates directory.Then set the redirect links for log in and log out, which will both go to our
home
template. Add these two lines at the bottom of the file.Create a new project-level templates folder and within it a
registration
folder as that's where Django will look for the log in template. We will also put our signup.html
template in there.Then create four templates:
Update the files as follows:
Now for our
urls.py
files at the project and app level.Create a
urls.py
file in the accounts
app.Then fill in the following code:
Last step is our
views.py
file in the accounts
app which will contain our signup form.Ok, phew! We're done. Let's test it out.
Start up the server with
python manage.py runserver
and go to the homepage at http://127.0.0.1:8000/.Click on Log In and use your superuser credentials.
Upon successful submission you'll be redirected back to the homepage and see a personalized greeting.
Now use the logout link and then click on signup.
Create a new user. Mine is called
testuser
. After successfully submitting the form you'll be redirected to the login page. Log in with your new user and you'll again be redirected to the homepage with a personalized greeting for the new user.Django Custom Test Runner Free
If you want to peruse the admin log into it with your superuser account at http://127.0.0.1:8000/admin. If you look at
Users
you can see our two users.Conclusion
Now that our custom user model is configured you can easily and at any time add additional fields to it. See the Django docs for further instructions.
You can also check out DjangoX, which is an open-source Django starter framework that includes a custom user model, email/password by default instead of username/email/password, social authentication, and more.
#26981closedNew feature (fixed)
Reported by: | Owned by: | ||
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Change History (6)
comment:1 Changed 5 years ago by
comment:2 Changed 5 years ago by
Owner: | changed from nobody to Chris Jerdonek |
---|---|
Status: | new → assigned |
Version: | 1.9 → master |
comment:3 Changed 5 years ago by
Django Run
comment:4 Changed 5 years ago by
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Uncategorized → New feature |
Django Test Client
comment:5 Changed 5 years ago by
comment:6 Changed 5 years ago by
Django Custom Test Runners
Note: See TracTickets for help on using tickets.