Django Registrations using User model - python

I'm trying to replicate Groupon using Django. I'm basically trying to achieve the below
1. A Merchant can register with the site
2. Can start creating campaigns
For the merchant registration part , I'm importing the User model and including a random MerchantID generator using random.
class Merchants(models.Model):
merchant = models.OneToOneField(User)
MerchantID = models.CharField(max_length = 15)
Storename = models.CharField(max_length = 25)
def save(self):
self.MerchantID = MerchantIDgen()
super(Merchants,self).save()
def __str__(self):
return self.merchant.username
The merchant is able to successfully register with the site. However, I'm having trouble enabling the create campaigns part.
the html link to the create campaign part is:
{% if user.is_authenticated %}
<a href = '/Merchants/{{user.username}}/Campaign'> start a campaign </a><br/>
ideally , I would want the variable portion to pull in the MerchantID value from the user. But I cant seem to pull this value from the User model( which makes sense since its sitting in the Merchants table)
In order the accomplish the above, is it better for me to just scrap the User model and just do the registration from the Merchant Model alone? (am I right in assuming that by doing so , I wouldn't have access to user authentication etc?)
Or is there any other way to pull the MerchantID using the user model?

figured a workaround which basically is to filter using username from user and using that instance to filter the Merchants table

Related

Display registered person values to another html page in django

I have member table for registration.I want to display that table values(like firstname,lastname, email etc...) in another page (only registered person values )
I tried with id. if I give id means only that particular value is displaying but I want to registered person values
This is my member table
class Member(models.Model):
firstname=models.CharField(max_length=30)
lastname=models.CharField(max_length=30)
Email=models.CharField(max_length=50)
password=models.CharField(max_length=12)
Thanks for your time.
you are talking about a registered person in your app but there is no field in your models to note which one is registered! so First, you should make a decision about your members' login method and keep some data about them. one simple way is to make another table to save registered members, like this, may be useful.
class RegisteredMember(models.Model):
Member = model.ForeignKey(Member,on_delete=models.CASCADE)
#and other data you need to save
So now you can manage your registered members and find them and get data about them all things you need to do.
You need to add a user field so that you can get the data of a logged in user
from django.contrib.auth.models import User
class Member(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
firstname=models.CharField(max_length=30)
lastname=models.CharField(max_length=30)
Email=models.CharField(max_length=50)
password=models.CharField(max_length=12)
In your view to get the logged-in user data, you can use request.user
member = Member.objects.get(user=request.user)
print(member.firstname)

How can I lookup a value based on user input and and use display it to the user in django

I'm currently learning to develop websites using Django. I have the following problem, where I have two models:
class Model_1(models.Model):
id = models.Charfield(max_length=250)
account_name = models.Foreignkey(Accounts, on_delete=models.CASCADE)
class Model_2(models.Model:
account_name = models.Charfield(250)
account_type = models.Charfield(250)
I have a html form where the user inputs their id, and chooses the account name. I would like that to redirect them to a new html page where they can see the account type that is associated with that account name. What is the best way to do this?

django - How to make a simple shopping cart

I'm at a loss. I've searched most of the links on Google/YouTube and can't seem to figure out how to create a simple session-based cart in Django!
My website is NOT an e-commerce store. I have a Post model which users can review via a Comment model. They review the Post's overall quality, overall difficulty, and workload based on a 1-5 rating system.
All that I want is for users to be able to go to the Post page, press "Add to Cart" and be redirected to a "cart.html" page where they see the Post they just added, as well as that Post's ratings (Quality, Difficulty, Workload). These ratings come from my Comment model, which is 100% based on User input.
I need this cart to exist in the session! It is very important that anonymous users have access to this feature.
here's a brief snippet of my models.py
class Post(models.Model):
[...]
title = models.CharField(max_length=250)
# rest of code
class Comment(models.Model):
[...]
post = models.ForeignKey(Post, related_name="comments")
user = models.ForeignKey(User, related_name="usernamee")
[...]
overall_rating = models.FloatField(choices=overall_rating_choices,
default=None)
difficulty_rating = models.FloatField(choices=difficulty_rating_choices, default=None)
workload_rating = models.FloatField(choices=workload_rating_choices,
default=None)
# rest of code
I don't need a checkout page, I don't need any payment processing, I don't need any price calculations for any of my posts and I don't need quantities at all. Each post should only be added once and that's it.
This is a special use case and I'm afraid my Django skills aren't up to par to tackle this yet.
I would ask if anybody can give me some sort of guidance on this issue?
P.S. I have been doing Django for 2 months now. I'd say my skill level is at about a 2/10.

django admin, displaying all instances of a model1 associated with model2 on model2's change page?

Consider these two models Keyword and Statement (model1 and model2 respectively):
#python_2_unicode_compatible
class Keyword(models.Model):
word = models.CharField(max_length=200)
statement = models.ManyToManyField(Statement)
def __str__(self):
return self.word
#python_2_unicode_compatible
class Statement(models.Model):
statement_id = models.CharField(max_length=200)
title = models.CharField(max_length=200)
issue_date = models.DateField("Issue-Date")
author = models.ForeignKey(Person)
released_by = models.ForeignKey(Organization)
kicpairs = models.ManyToManyField('KeywordInContext')
So on the admin site right now, the only way one would be able to determine what keywords are associated with each statement is that they have to go check the Keyword model in admin, and check each Keyword's display page and scroll through the menu.
At least with regards to the admin site, it's important for someone to be able to see a Statement model's display with all of its associated Keywords visible, and for users to be able to choose additional Keywords within the database (or make new ones). I also hope to be able to have a Statement's keywords modifiable on the admin page via the filter_horizontal widget, since that seems to be the most user friendly.
But I'm having trouble just starting with that. I'm not sure what I need to use or how.

Django - Restricting User Access to the Admin Site

I am trying to create an app for tracking team rosters that is based in the django admin (ie localhost/admin).
I would like to set it up so that I can assign certain teams to certain users, such that when they log in to the admin site, they can only see and edit the teams they have been assigned to.
I would like a user with superadmin status to be able to add and remove user access to various teams through the admin site, without having to modify the underlying code each time a new user or team is added. Is there a way to do this?
class Team(models.Model):
team_name = ...
# Whatever other attributes.
class Player(models.Model):
first_name = ...
last_name = ...
team = models.ForeignKey(Team)
# Whatever stats you want to keep on the players
You can dynamically change the query set used by the admin; you could do this to only show teams that are associated with the logged-in user.
class TeamModelAdmin(admin.ModelAdmin):
def get_queryset(request):
return Team.objects.filter(...)

Categories