This assignment is designed for you to practice using forms and authentication, as well as writing unit and route tests for your code.
To complete this assignment, you can and should reference your code for Assignment 4: Polls Redux. This assignment is for the most part very similar, and is designed to be a check for understanding.
Scoring is as follows:
Criteria | Possible |
---|---|
Login & Logout | 10 |
Signup | 10 |
Wiki Article Creation Form | 10 |
Wiki Article Edit Form | 10 |
Wiki Details Page Test | 10 |
Wiki Details Page Edit Test | 10 |
Wiki Creation Page Test | 10 |
TOTAL | 70 |
Go to the MakeWiki starter code and clone the repository to your local hard drive. Then, set up a new repository for your application on GitHub.com and execute the following to switch the remote:
$ git remote remove origin
$ git remote add origin git@github.com:USERNAME/REPOSITORY.git
Run the server and take a look at the site: there is a Wiki List page which lists the wiki articles, and a Wiki Detail page which lists specific details about each one.
In this part, we'll add the login, logout, & signup functionality to the MakeWiki app.
- Add the Django authentication views in
django.contrib.auth.urls
to the project's URLconf. - Test
/accounts/login
in your browser -- see if you can log in using usernameadmin
and passworddjangopony
. - In
base.html
, hide the New Page and Log Out buttons for unauthenticated users. - When a user clicks
Log Out
, they should be logged out of the system, and returned to the homepage. - When a user clicks
Log In
, they should be navigated to the Login page. - Replace
Hello, USERNAME
text with the username of the logged-in user, and only show it when the user is logged in.
- Create a new app named
accounts
. Add it to theINSTALLED_APPS
list insettings.py
. - Create a view for signing up that extends
django.views.generic.CreateView
and renders the template contained inregistration/signup.html
. - In accounts/urls.py, link the
SignupView
class to a URL namedsignup
. - Add
accounts.urls
to the project's URLconf. - When a user clicks
Sign Up
, they should be presented with a form to create an account, and can submit it successfully.
Here we'll flesh out the functionality of the "Create" button, as well as provide an Edit option to edit existing wiki articles.
To add a Create view, complete the following steps in the wiki
app:
- In
views.py
, import theArticleForm
class fromforms.py
. This form enables editing and creating ofArticle
objects in the database. - Create a new view class called
ArticleCreateView
. Link it to a URL namedcreate
. - On
GET
, render a template to display theArticleForm
instance. - On
POST
, check if the data in the form is valid.
- If True, save the data and use the logged-in user as the
Article
'sauthor
. Then, redirect to the Detail View for the newly createdArticle
object. - If False, display all the errors in the template, above the form fields.
- Instead of hard-coding the path to redirect to, use the
reverse
function to return the path.
For the Article Detail view, complete the following steps in the wiki
app:
- In
views.py
, import theArticleForm
class fromforms.py
. Edit theArticleDetailView
class as follows: - On
GET
, render an edit form below the article details. - On
POST
, check if the data in the form is valid.
- If True, save the data and use the logged-in user as the
Article
'sauthor
. Then, redirect back to the Detail View. - If False, display all the errors in the template, above the form fields.
- Instead of hard-coding the path to redirect to, use the
reverse
function to return the path. - (Stretch Challenge) After successfully editing an Article, use Django Messages to "flash" the user a success message: "REPLACE_WITH_ARTICLE_TITLE has been successfully updated."
- (Stretch Challenge) Use the Crispy Forms library in conjunction with Bootstrap to make your forms look even more beautiful and clean (and, well, crisp)!
In this part of the assignment, we'll flex our testing muscles and write some route tests!
Write a route test that does the following:
- Create an instance of
Article
and save it to the database. (HINT: You'll need to first create an instance ofUser
to use as the article's author.) - Load the Wiki Details page for that
Article
'sslug
. - Verify that the response has a
status_code
of200
. - Verify that the response's context includes the info for the
Article
object requested. (HINT: You can useassertContains
to check the exact text of the response.)
Write a route test that does the following:
- Create an instance of
Article
and save it to the database. - Create a dictionary of key-value pairs containing the post data to be sent via the form. This should include a new article title and description.
- Make a
POST
request to the details page withself.client.post()
. - Check that we get a 302 status code. (Why 302 and not 200?)
- Check that the article object was modified in the test database.
Write a route test that does the following:
- Create a dictionary of key-value pairs containing the post data to be sent via the form. This should include an article title and description.
- Make a
POST
request to the Wiki Create page withself.client.post()
. - Check that we get a 302 status code.
- Check that the article object was created in the test database.
Submit your finished homework using our class submission site.