Created
April 4, 2012 03:06
-
-
Save SmileyChris/2297452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Use 4 spaces, not tabs :P | |
| # Don't go overboard with your newlines. | |
| from django.shortcuts import render, redirect | |
| from django.core.urlresolvers import reverse | |
| # Do global imports rather than local ones unless you're specifically avoiding | |
| # a circular import. | |
| # Personally, I prefer to import the module rather than the classes. | |
| from pcddb.profiles import forms | |
| from pcddb.profiles import models | |
| # You probably want more than login_required! Check the permissions decorators. | |
| @login_required | |
| def admin_modify_user(request, p_id): | |
| u = get_object_or_404(User, pk=p_id) | |
| # You named your url pattern, right? | |
| cancel_target = reverse('users-edit') | |
| # Get the user info, or generate an unsaved one. | |
| # We're limiting the try/except specifically to the action which could | |
| # raise it. | |
| try: | |
| # Potentially you could be using the reverse one-to-one relationship | |
| # (I'm guessing at your model structure, but it makes sense) instead. | |
| up = models.UserInfo.objects.get(user=u) | |
| except models.UserInfo.DoesNotExist: | |
| up = models.UserInfo(user=u) | |
| if request.method == 'POST': | |
| data = request.POST | |
| else: | |
| data = None | |
| # Always use keyword arguments for your forms. | |
| addform = forms.AdminEditUserForm(data=data, instance=u) | |
| addform2 = forms.AdminEditUserInfoForm(data=data, instance=up) | |
| # Note that an unbound form is never valid, so we can rely on data=None | |
| # meaning this won't pass. | |
| if addform.is_valid() and addform2.is_valid(): | |
| addform.save() | |
| addform2.save() | |
| return redirect(cancel_target) | |
| # Use the render shortcut instead of render_to_response to get a | |
| # RequestContext automagically. | |
| # It's probably better to just use a specific template for this. | |
| return render(request, "generic_form.html", | |
| # Never use locals() | |
| dict(page_title="Modify User Account", button_text="Modify", | |
| cancel_target=cancel_target)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment