Monday, June 26, 2017

Create media directory for each user upon user registration

Hey all,

I've been trying to figure out how to create a directory in my media directory for each user upon user registration.  I have found a lot of resources on how to upload files to a directory but just about none on simply how to create the directory.  I would like to be able to have my webapp create xml files for each user and then place them in the user's own directory.  The second part I am fine with.  Does anyone have any suggestions on how to go about adding a directory attribute to a UserProfile model?  I can't seem to get anything to work.

Here's some code.  Note that it does not have any directory information applied to the code (other than that little line in models.py) because I really have no idea how to go about this.

models.py
class UserProfile(models.Model):
    user
= models.OneToOneField(User)
    directory
= # something here to have an established user directory

views.py
def register(request):
   
# A boolean value for telling the template whether registration was successful.
   
# Initially set to False.  Code changes to True when registration succeeds.
    registered
= False

   
   
if request.method == 'POST':
        user_form
= UserForm(data=request.POST)
        profile_form
= UserProfileForm(data=request.POST)
               
       
if user_form.is_valid() and profile_form.is_valid():
            user
= user_form.save()
            user
.set_password(user.password)
            user
.save()
            profile
= profile_form.save(commit=False)
            profile
.user = user
            profile
.save()            
            registered
= True
            username
= request.POST['username']
            password
= request.POST['password']
            user
= authenticate(request, username=username, password=password)
           
if user is not None:
                login
(request, user)
               
return HttpResponseRedirect(reverse('elsa:index'))
           
else:
               
return HttpResponse("Error in login after registration.")                
       
else:
           
# Invalid form or forms - mistakes made
           
print(user_form.errors, profile_form.errors)
   
else:
       
# Not an HTTP POST, so we render our form using two ModelForm instances.
       
# These forms will be blank and ready for user input.
        user_form
= UserForm()
        profile_form
= UserProfileForm()

   
# Render the template depending on the context.
   
return render(request, 'elsa/register.html', {'user_form': user_form,    
                                                 
'profile_form': profile_form,
                                                 
'registered': registered})

forms.py
class UserForm(forms.ModelForm):
    password
= forms.CharField(widget=forms.PasswordInput())
   
#data_relation = forms.(max_length=8, default='provider', help_text='Choose provider or reviewer')
 
   
class Meta:
        model
= User
        fields
= ( 'username', 'email', 'password') #, 'data_relation')



class UserProfileForm(forms.ModelForm):

   
class Meta:
        model
= UserProfile
        exclude
= ('directory', )  # see note on models.py


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ae06e7f-6c93-4e26-8138-4515788f3a6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment