I have a toggle button in my frontend called ‘Email on Signup’. As the name suggests, this button when toggled on allows to send a mail to the registered email after the account is registered notifying that the account has been registered. I have a table named EmailStatus, which has fields, condition_title and condition status. The first row of the table has mail_on_signup as the condition_title and False as condition_status.
My question is, currently I’m making it so that when the button is toggled ‘ON’, it queries the database and changes the condition_status of ‘mail_on_signup’ to true, which allows the view to send the mail after the signup is complete. Is there a better way to do this without querying the database because the approach seems really bad? Also querying the database like,
EmailStatus.objects.filter(condition_title=‘mail_on_signup’).first()
seems a bit odd
Also, I cant seem to think of any other approach except for variables in my settings.py, but it is meant to be static. So basically nothing. Any suggestions guys?
this is the view i think that most clearly specifies what i want to do
Welcome @UzitheI !
First a side note. Please do not post images of code here. Copy / paste the code into the body of your post, and enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.
Why does it seem bad? What is it about this approach that bothers you?
(In the general case, worrying about such things tends to be counter-productive. If this is a one-time event, it’s not like it’s going to be a continuous performance issue on your system. In other words, it might not be the “optimal” approach (for some definition of “optimal”), but is it really a problem that you need to spend time on? Please note that I am not making a judgement call either way, I’m not saying it’s good or bad, just that you’ve probably got more important things to worry about at this time.)
That’s probably not what you want to do here, but I can’t quite read your code to figure out what it is that this query is supposed to do in the context of your view.
Thank you…I’ll make sure I dont post the screenshot of my code and follow your guidelines from next time.
I feel the approach is bad because I feel like mail_on_signup should be a variable and I just dont feel good to keep it inside the database. I also cannot find a way to update this variable to be True or False from the frontend without keeping it in the database.
EmailStatus.objects.filter(condition_title=‘mail_on_signup’).first()
this particular query as you can see in the try block retrieves the row of the database with the title mail_on_signup and im checking to see whether the condition_status of this row is True or False
If it is True, an email will be sent to the user after the registraion is complete,if false the email is not sent after the registration
I just find it to be odd and I feel like there must be a better way to do this.
It’s an attribute of the user. Why wouldn’t it be in the database?
I’m sorry, I can’t read the code in the posted image.
If you post the complete view here, I may be able to offer a suggestion.
class CustomSignupView(AllauthSignupView):
def form_valid(self, form):
print('apple is working')
self.user = form.save(self.request)
if not self.user:
return get_adapter(self.request).respond_email_verification_sent(
self.request, None
)
try:
email_status = EmailStatus.objects.filter(condition_title='mail_on_signup').first()
if email_status and email_status.condition_status:
return complete_signup(
self.request,
self.user,
app_settings.EMAIL_VERIFICATION,
self.get_success_url(),
)
else:
self.user.is_active = True
self.user.save()
# Authenticate and log in the user
user = authenticate(self.request, email=self.user.email, password=form.cleaned_data.get('password1'))
if user is not None:
login(self.request, user) # Log in the user
# Redirect to the customer dashboard
return redirect('dashboard:customer_dashboard')
except ImmediateHttpResponse as e:
return e.response
I’m moving on with this approach for now Thank You For Your Help. Also, do you suggest resources to beginners. If you do, what would be the best place to learn Ajax and its implementation with django. Also can you suggest me what the flow should be like when coding in django? I think it is more of a programming in general question but how much time do you think should you spend on actually planning what to code or do you just code and deal with problems as they come? The latter seems to be more reasonable but I always end up rewriting my code as I feel like the newer approach i thought during coding is more better.
I have been seeing your answers here for a long time now so I’m asking this. Sorry, if this is not the right place to ask general questions.
Specific ones, no. However, I recommend people select resources from those listed in the Education section of the Awesome Django page.
From Django’s perspective, AJAX is just another request. It doesn’t matter to Django whether the request is coming from a browser or JavaScript, a request is a request. The only common difference in practice is whether the view handling that request is going to return a response containing html or json.
In part it depends upon the type of project and the reason for the project. My general approach varies greatly depending upon the “why”. (i.e., My process differs depending upon whether I’m working on a hobby project, a “small” project for work, or a “large” project for work. Note, the difference between the last two is determined by the projected number of users, not the amount of code involved. I deploy a fair number of projects where the expected number of users = 2 or 3 - and I’m one of them.)
It’s never a bad idea to identify where you may have been able to do things better. And, it’s always a good idea to remember to make those improvements the next time you’re faced with a similar situation.
However, whether it’s worth your time and effort to actually change existing code is a completely different question. Sometimes “good enough” is good enough, and the right call is to acknowledge that and move on.
No worries. This is your topic, and I tend to be lenient with general side-bar discussions about more general topics if they have grown organically out of the base discussion. (The other moderators may have a different opinion, and if they do, they’ll let me know.)