I am new and trying to display the image of user after he logs in. I am using the django user model which is a Foreign key to my extended user model which is called Consumer. I am using auth.views to authenticate the user and generate view. I able to get the username but not able to retrieve the image URL as I am not able get the Consumer instance. Where and how can I extract the Consumer detail for the logged in user without writing my own authentication code.
Please help. Please see my code below
Models.py
class Consumer(models.Model):
user = models.OneToOneField(User)
cimage = models.ImageField(blank=True)
company = models.CharField(max_length=32)
urls.py
(r'^login/$', 'django.contrib.auth.views.login'),
screen.html
If you're getting the username via {{ user.username }} you can just do the same thing to get to the consumer: {{ user.consumer.cimage }}.
Related
I am attempting to write an admin action that accesses data from selected users. i.e. user's email. However, I have only been able to access the instance/data of the user that is currently logged in.
For example, to access the emails of selected users, I have tried:
#models.py
class Account(AbstractBaseUser):
email = models.EmailField(max_length=60, unique=True)
#admin.py
from account.models import Account
for Account in queryset:
author = request.Account.email
#OR
author = Account.objects.get(email=request.email)
print(author)
and both of these will fill "author" with the email address of the admin that is trying to pull the data.
Does anyone know how I could pull data from selected accounts with an admin action?
I was really overcomplicating it. Ironically enough, I found the answer on this site called simpleisbetterthatcomplex. The proper format was
for Account in queryset:
print(Account.email)
I´m working on an Django Webpage and need some help regarding my Usermodel.
I´ll already have a Login for the both Producer & Customer that looks like this:
class LoginForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User= ['username', 'password']
fields
So iff you open /producer/login or /customer/login you can login with a set name and passwort stored in the mysql DB
Now I want to change the login to a kind of global login where you have just one login page that redirects you wether you are a producer or customer.
Can anybody help me or give me a hint how i can implent this?
I red this article Restricting User access to different apps in Django but wasn´t sure if this is what I need.
Thanks for your help!
In my settings.py I have AUTH_PROFILE_MODULE = 'accounts.MyProfile'
but in my models.py
class Post(models.Model):
moderator = models.ForeignKey(User)
Maybe I should've used MyProfile there instead of User. Perhaps I'm having this problem:
In my views.py
profile = post.moderator.__class__.objects.get(username=post.moderator.username)
and with that profile I have the following in my template
<img src="{{ profile.get_mugshot_url }}" /></span> </div>
This does not show anything. but if I were to do this in views.py
profile = MyProfile.objects.get(user_id=request.user.id)
I get an image of my profile. I should be getting the image of post.moderator but I get mine. any help please,what should I do, where did I mess up
I don't understand why you would do the query like that. The moderator is the user, so there doesn't seem to be any good reason to go back to the class and query by username; you already have the user.
Your query would not give you the UserProfile in any case. If you want that you just need to follow the relation from User to UserProfile:
profile = post.moderator.userprofile
assuming you have a one-to-one field from UserProfile to User.
Also note AUTH_PROFILE_MODULE has long been removed; and it wouldn't have done anything in this case anyway, as it was only invoked when you called get_profile and you weren't.
I've been advised that when it comes to updating a user I should use Django forms rather than rolling my own. As such, I've turned to Django forms but hit a bit of a wall.
A user in my system is defined, partly, as so:
#models.py
class Freelancer(AbstractBaseUser):
email = models.EmailField(primary_key=True)
first_name = models.CharField(max_length=128)
surname = models.CharField(max_length=128)
university = models.CharField(max_length=256)
verified = models.BooleanField(default=False)
biography = models.TextField(default="")
skills = models.ManyToManyField(Skill)
created_date = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
I have a URL:
#urls.py
url(r'^profile/$', views.Profile.as_view(), name="profile"),
And the view:
#views.py
class Profile(UpdateView):
model = Freelancer
fields = ['email']
template_name_suffix = '_update_form'
The problem is that I need to tell the server what Freelancer I want to update. I've tried adding (?P<pk>\d+)/ into the URL regex as I saw on a few tutorials but when I pass in the id of a Freelancer - as automatically created in the schema - it doesn't retrieve a Freelancer. Based on my model, what should I do?
I think it's a bad idea to use the email address as primary keys. What if a user changes their email address?
It might be a better idea to have unique=True for the email address, and let Django create the automatic primary key. Then including (?P<pk>\d+)/ in your url should work.
If you must use the email as the primary key, you need to change the regex from (?P<pk>\d+)/, which will only match digits, to something like
(?P<pk>[\w#.-]+)
The above might not catch all email addresses. I think Django contains a better character class, but I can't find it at the moment.
I think you are just doing it in a wrong way.
Here is what you need to do:
First of all I'd recommend you to add a pk to your model, as having email as a pk is a bad idea.
Create a url for update view
An example:
url(r'^profile/(?P<pk>[\d]+)/edit/$',
views.ProfileUpdateView.as_view(), name='edit_profile')
Create an UpdateView
An example:
class ProfileUpdateView(UpdateView):
model = Freelancer
fields = ['email']
template_name = "profiles/profile_edit.html"
Create a template for the form
An example:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
</form>
Now you need to add this link wherever you want:
Edit
When you vist the url you will go to update page.
[Django 1.5.1]
I've set up django-profiles and django-registration for my small site in a way that involves a 'custom' registration backend such that during registration, a new user fills out their username, password, and profile fields in one go. But I'm now trying to allow users to log in with Facebook and trying to use django-facebook. As far as I understand, logging in through django-facebook's Facebook login will authenticate someone, but that won't create a user object nor a profile object.
So the idea I had in mind would be to add an extra profile field which holds a user's potential Facebook ID(which I believe is unique) if they want to use Facebook. Then force a check on every template if the user has a profile or not, and if not, to direct them to the 'create profile' page. Then whenever a user logs in through the Facebook login, it'll somehow link their session with the corresponding profile object which matches the Facebook ID (and consequently the user object corresponding to the profile object). I think I'd have to apply a filter and then create a 'signal', but I'm not too sure how to do that.
This sounds very convoluted though. How might I be able to get this accomplished the right way?
Here's how I suggest you do things. Do away with django-facebook and look into django-allauth. It will handle accounts (registration, logic, connecting social accounts).
Also, django-profiles has many issues with 1.5+ for me and I don't bother with it and instead create my own profiles app and UserProfile model with any additional fields that wouldn't be handled by django-allauth.
An example from one of my implementations
class UserProfile(models.Model):
user = models.OneToOneField(User)
default_address = models.OneToOneField(Address, blank=True, null=True)
default_tshirt_size = models.CharField(blank=True, null=True, choices=constants.tshirt_sizes, max_length=50)
default_shoe_size = models.CharField(blank=True, null=True, choices=constants.shoe_sizes, max_length=50)
Your user profile model should be pointing to the same User model allauth is using.
from allauth.utils import get_user_model
User = get_user_model()
Here's a neat method I use to create the User's profile automatically if it hasn't been already.
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
Then just create a sign-up form class with a save() method that takes user as an argument. Link to this class in your settings file.
ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm'
See this post on for a somewhat relevant example, except of course with your implementation you'll probably want to get the UserProfile based on the user.id at that point, and save the additional values to the profile instance.
How to customize user profile when using django-allauth