hello everyone,
I have a chat menu that works like the following:
each room has a chat
each room has his own messeges refered to the room id, for example room number 1 has 5 mesages that all point to him.
long story short, my code needs to send a request to this url:
url : “/getMessages/{{course.id}}”,
it all works,
but if i change it to this code:
url : “{% url ‘getMessages’ course_id={{ course.id }} %}”, it gives me an error
but if I do the following:
url : “{% url ‘getMessages’ course_id=1 %}”,
it works, (the problem is in the way django import the id not the url,
any ideas?
Yes, review the docs for the url template tag.
thanks, i’ll look into it.
another question,
i have a page with this url:
path("courses/<int:course_id>/lecture/<int:lecture_id>",
views.lecture_page, name="lecture_page"), # spesefic lecture in a course
and i want to acsses the lecture_id in a temlate like so:
{% if lecture.id=request.lecture_id %}
<div class = "lecture_section currect_lecture">
how can i do it?
thanks
All the variables being used in a template must be supplied to the template in the context.
Also see Boolean operators
hey, I have a problem with querrying.
I have a bundle that contains many courses, in a many to many relasionship. (course can have many bundles).
i have a course object and i want to get the bundle that contians him at a lenght of one. (will be only 1).
i tried doing this, temp_course.course_bundles.filter(len(bundle_content)=1)
(temp_course is the object im holing, course_bundles to acsses all the bundles this course is in)
can i do this querry with filter or do i have to make a loop and run over the course_bundles that contain this course.
thanks!
Trying to rephrase this for clarity -
Given a course object, you want to get all bundles referencing that course object, where the bundle only has 1 reference to courses.
Is this what you’re trying to do?
another question,
I am trying to send an email, after following the documantion and some youtube toturials I wrote code and i get the following erorr,
File "C:\Program Files\Python39\lib\socket.py", line 954, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11003] getaddrinfo failed
settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp@gmail.com'
EMAIL_HOST_PASSWORD = '----'
EMAIL_HOST_USER = '----'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
view:
from django.conf import settings
from django.core.mail import send_mail, BadHeaderError
def password_reset_request(request):
if request.method == "POST":
password_reset_form = PasswordResetForm(request.POST)
if password_reset_form.is_valid():
data = password_reset_form.cleaned_data['email']
associated_users = NewUser.objects.filter(Q(email=data))
if associated_users.exists():
for user in associated_users:
subject = "Password Reset Requested"
email_template_name = "main_website\password_reset_email.txt"
c = {
"email":user.email,
'domain':'127.0.0.1:8000',
'site_name': 'Website',
"uid": urlsafe_base64_encode(force_bytes(user.pk)),
"user": user,
'token': default_token_generator.make_token(user),
'protocol': 'http',
}
email = render_to_string(email_template_name, c)
try:
send_mail(subject, email, settings.EMAIL_HOST_USER , [user.email], fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid header found.')
messages.success(request, 'A message with reset password instructions has been sent to your inbox.')
return redirect ("main_website_page")
password_reset_form = PasswordResetForm()
return render(request=request, template_name="main_website\password_reset.html", context={"password_reset_form":password_reset_form})
the problem happends when sending the mail.
also gmail confugoration
seems like gmail side problem, but it cant be true
still dosent work,
if it helps when i use smtplib I can send emails via the same adress.
any ideas?
I’m pointing you to compare your settings within your Django/Python code to the settings required by gmail.
thanks!
another question i coudnt find answer to,
I have a course object and a bundle that holds many courses.
each time i create a course i automaticly creata a bundle holding that course alone.
I want to make that when i update a course i automatlicy update the bundle that holds that course.
code:
objects:
class course(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=500)
title_description = models.CharField(max_length=150,default="")
price = models.FloatField()
image = models.ImageField(upload_to="uploads")
course_sections = models.ManyToManyField(course_section,related_name="course")
autor = models.ForeignKey(NewUser, related_name='writen_courses',
on_delete=models.DO_NOTHING, blank=True, null=True)
degree = models.ManyToManyField(degree,related_name="courses")
def __str__(self) -> str:
return f"{self.name} - {self.price}$"
def save(self, *args, **kwargs):
if self._state.adding: # create
super(course, self).save(*args, **kwargs)
course_bundle = bundle(name=self.name, description=self.description,
price=self.price, image=self.image,title_description=self.title_description,one_to_one_course=self)
course_bundle.save()
course_bundle.bundle_content.add(self)
else: #updating
super(course, self).save(*args, **kwargs)
super(bundle,self.one_to_one_bundle).save(*args,**kwargs) ## problem here
class bundle(models.Model):
name = models.CharField(max_length=50)
title_description = models.CharField(max_length=150,default="")
description = models.CharField(max_length=500)
price = models.FloatField()
image = models.ImageField(upload_to="uploads")
bundle_content = models.ManyToManyField(
course, related_name='course_bundles', blank=True)
one_to_one_course = models.OneToOneField(course,blank=True,on_delete=CASCADE,related_name="one_to_one_bundle",null=True)
def __str__(self) -> str:
return self.name+" - id:"+str(self.id)
how do i do that?
I’m not clear as to what you’re saying needs to be updated here.
Also, it makes no sense to me for you to have two relationships between bundle and course, one as a many-to-many and another as a one-to-one. I think you’re going to avoid problems going forward if you clean up your model relationships.
basicly i have courses, for example caclus one, algebra one etc, and I have bundles which contains many coureses (or one) for example first semester courses.
now the way the data is stuctered is that every course has his own bundle (that contains only him) and there is more bundle that have many courses.
for example if i have 3 courses, say A B C i will have bundles A B C and might have a bundle that contains them all. (I made this stucture for the way i handle purchses).
now when I create a new course I want to automaticly create a bundle that contains only him. and when i update that course I want the bundle to update to.
so overrided the save method on course class,
def save(self, *args, **kwargs):
if self._state.adding: # create
super(course, self).save(*args, **kwargs)
course_bundle = bundle(name=self.name, description=self.description,
price=self.price, image=self.image,title_description=self.title_description,one_to_one_course=self)
course_bundle.save()
course_bundle.bundle_content.add(self)
else: #updating
super(course, self).save(*args, **kwargs)
super(bundle,self.one_to_one_bundle).save(*args,**kwargs)
first first section (the if) will create a bundle if a new course is created, the else suppose to handle the course update, I want uppon an update to update the bundle that is in a one to one relasion ship with the course.
So you’re close with your else clause.
Try something like:
else:
super().save(*args, **kwargs)
# Update the related bundle
self.one_to_one_bundle.name = self.name
self.one_to_one_bundle.description = self.description
...
self.one_to_one_bundle.save()