Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How To Create An Article Parser Loop With Apscheduler in Django
I have a script in Django that helps me parse RSS feeds for my own daily consumption. It gets the jobs done so far. Its made 3 models: class Categories(models.Model): category_name = models.CharField(max_length=50, verbose_name='Categories') ''' Categories = ['Business', Sports, Politics, Tech] ''' class FeedSource(models.Model): feed_url = models.URLField(verbose_name='RSS Feed Link') ''' FeedSource = ['http://rss.cnn.com/rss/edition.rss', ''https://www.espn.com/espn/rss/news, 'http://rss.politico.com/politics.xml'] ''' class Posts(models.Model): ... source = models.ForeignKey(FeedSource, on_delete=models.CASCADE, verbose_name='Source') category = models.ForeignKey(Categories, on_delete=models.CASCADE, verbose_name='Category') The Apscheduler command is as follows. As you can see, there is a bit of repetition in the fetch_articles functions that parse different links because each link belongs to a different Feedsource & Category. The same applies to Handle function. Each fetch_articles function has its own scheduler. import logging from django.conf import settings from django.core.management.base import BaseCommand from apscheduler.schedulers.blocking import BlockingScheduler from django_apscheduler.jobstores import DjangoJobStore from Posts.models import Posts, FeedSource, Categories import time logger = logging.getLogger(__name__) def save_new_articles(feed, source_id, category_id): """Function that parses & saves new articles to DB""" ... if not Posts.objects.filter(guid=item.guid).exists(): post = Posts( ... guid = item.guid, source_id = selected_source_id category_id = selected_category_id ... ) post.save() def fetch_siteA_articles(): """Fetches news artices from siteA""" feed = feedparser.parse("siteA") source = FeedSource.objects.get(pk=1) source_id = int(source.pk) category = Categories.objects.get(pk=3) category_id = int(source.pk) … -
Django using a Query in a Query does not work
so I have this model : class Student(models.Model): id = models.AutoField(primary_key=True) first_name=models.CharField(max_length=50) last_name=models.CharField(max_length=50) title=models.CharField(max_length=30,default="",blank=True) tel=models.CharField(max_length=50,default="",blank=True) e_mail=models.CharField(max_length=50,default="",blank=True) social_media=models.CharField(max_length=50,default="",blank=True) social_media2=models.CharField(max_length=50,default="",blank=True) social_media3=models.CharField(max_length=50,default="",blank=True) street=models.CharField(max_length=50) postal=models.CharField(max_length=50) city=models.CharField(max_length=50) country=models.CharField(max_length=50) graduation_uni=models.DateField(null=True,default=None,blank=True) graduation_ikomt=models.DateField(null=True,default=None,blank=True) certificate_no=models.CharField(max_length=50,default="",blank=True) def __str__(self): return str(self.id) + " - " +self.first_name + " " + self.last_name class Course(models.Model): id = models.AutoField(primary_key=True) course=models.CharField(max_length=50) description=models.CharField(max_length=50,null=True,default=None,blank=True) start_date=models.DateField(null=True,default=None,blank=True) end_date=models.DateField(null=True,default=None,blank=True) def __str__(self): return str(self.id) + " - " +self.course + " | " + self.start_date.strftime("%d.%m.%y") + " - " + self.end_date.strftime("%d.%m.%y") class StudentInCourse(models.Model): StudentId = models.ForeignKey(Student, on_delete=models.CASCADE) CourseId = models.ForeignKey(Course, on_delete=models.CASCADE) def __str__(self): return str(Student.objects.get(id=self.StudentId.id)) + " in " + str(Course.objects.get(id=self.CourseId.id)) And I'd like to Display only the Students in a specific course using Queries like this (in my views.py): students_in_course=StudentInCourse.objects.filter(CourseId=id) latest_student_list = Student.objects.filter(id__in=students_in_course) Sadly, I only get an empty result. Any Ideas what I'm doing wrong? I thought with the Primary/Foreign Key relation the Filter should just work this way? Any advice will be appreciated. Greetings -
How to use variable to get attributes of another another variable in Django templates
I am trying to populate a CSS grid in a Django template with specified columns and rows. To my best understanding, I need to do something like “nested variables” in the Django template to achieve what I’m looking for. I tried this, but it does not work: {% for parent_row in parent_rows %} {{ parent_row }} <div>p1</div> <div>p2</div> {% for child_row in children_rows %} <div> {{ child_row}} </div> {% for guy in guys %} <input type=”text” value=”{{guy}}.{{child_row}}”></input> {% endfor %} {% endfor %} {% endfor %} The {{guy}}.{{child_row}} neither some of the similar variants I tried do not work. I manage to get the result I'm looking for by just writing the whole HTML open, and then by looping through each guy on separate rows, as I can use for example guy.name to get the desired value to each row, but this means lots of repetition. parent_rows could look something like this for example: “names”, “contact_information”, “hobbies” children_rows could look something like this for example: “forename”, “family_name”, “phone_number”, “mail”, “favourite_hobby” and two records in the guys would be like this (both identical, to ease illustration): “forename”: “Mike” “family_name”: “McMediocre” “phone_number”: “123” “mail”: “mike@mediocre.com” “favourite_hobby”: “tennis” I am trying to populate … -
Django custom model manager for derived class - filtering on base class attribute
I am using Django 3.2 I have the following models: class AuthoriseableApprovedManager(models.Manager): def get_queryset(self): return super(AuthoriseablePendingManager, self).get_queryset().filter(computed_status=AUTHORISATION_STATUS_APPROVED) class Authorisable(models.Model): # various fields ... approved_objects = AuthoriseableApprovedManager() class ApprovedPublishedArticlesManager(models.Manager): def get_queryset(self): return self.approved_objects.filter(is_published=True) class Article(Authorisable, # Other base classes ): # various fields # .... objects = models.Manager() # The default manager. approved_published_articles = ApprovedPublishedArticlesManager() class Meta: ordering = ('-created_at', ) I have the following code in my views.py class ArticleListView(ListView): model = Article def get_queryset(self): return self.model.approved_published_articles.all() # <- Barfs here ... I get the error message: 'ApprovedPublishedArticlesManager' object has no attribute 'approved_objects' Which makes sense, since ApprovedPublishedArticlesManager does not derive from Authorisable. How do I create a custom manager that allows me to filter based on base class properties - does my custom manager have to derive from models.Manager and the relevant base class (Authorisable in this case)? -
Django app is crashing Chrome browser on Android
I have hosted a Django app on pythonanywhere.com . It works fine on desktop but crashes Chrome on Android. It works fine on another Android browsers and Safari too. It just a simple Django application with a single html page. One thing I found out was, it crashes only when I turn on 'Forced HTTPS' on pythonanywhere. If I turn it off it works fine. It's the bug with only this app. My other hosted apps works fine. Hosted app: https://shantanu2k17.pythonanywhere.com/ CAUTION : don't open it on Android chrome, your browser will crash. Open it in incognito mode instead. Code : https://github.com/Shantanu2k19/testing2.git Please help me find the bug. -
Django DRF - Send email with html template - django.template.exceptions.TemplateDoesNotExist
I've added sending emails in my Django project and now I want to use html templates for the content of these emails. I've created a templates folder and put my template in there. Then I added the template path to TEMPLATES in settings.py. (See code below) But now I'm getting the following error. django.template.exceptions.TemplateDoesNotExist: activate_email.html Even though the template exists: views.py # import file with html content html_version = 'activate_email.html' # 'templates/activate_email.html' also doesn't work html_message = render_to_string(html_version, {'context': context, }) email = EmailMessage( 'MySubject', html_message, 'info@example.co', [user.email], ) email.send() settings.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'django_backend/templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] Can anyone point me in the right direction what I'm doing wrong? I'm using Django 4.0 and DRF 3.13.1 -
Bootstrap 5 get rid of big number input fields
I have a table that can be used as a form, which all quite works well, but I do not manage to reduce the space a number input takes: here is a view from when it is not a form: What argument can I put in th class or td so the green areas get minimized? Fixed width in pixels would be an option (is this possible?). I already tried <th class="w-25"> or <td class="w-25"> (and both). I achieved adding a class="w-50" argument to the number input fields, so this seems to work. <table class="table table-striped table-hover"> <thead class="thead-dark"> <tr> {% for col in header %} {% if col == "number" %} <th>{{ col }}</th> {% else %} <th>{{ col }}</th> {% endif %} {% endfor %} <th class="text-end">action</th> </tr> </thead> <tbody id="table"> <tr> {% for key, value in product.data.items %} <td>{{ value }}</td> {% endfor %} </tr> </tbody> </table> -
How to add attributes in admin user model in django
I'm trying to add an attribute to my user admin model, but it doesn't work, here is what I succeded to do (I want to add inactivity field): from django.contrib import admin from .models import Patient, User,Group class usersAdmin(admin.ModelAdmin): list_display = ('username', 'first_name', 'last_name', 'inactivity') fieldsets = admin.ModelAdmin.fieldsets + ( ('Extra Fields', {'fields': ('favourite_colour',)}), ) -
Django form class widget with drop down populated from database
I am using Django 3.2 and I am trying to create a form class that has a field (category) populated from a database model. This is a snippet of my form class: class ArticleForm(forms.ModelForm): # ... class Meta: model = Article fields = ['title', 'teaser', 'category', 'guest_author','content', 'tags', 'is_published'] categories = [] #forms.ModelChoiceField(queryset=ArticleCategory.objects.all().order_by('name')) widgets = { 'guest_author': forms.TextInput(attrs={'class': 'form-control input'}), 'title': forms.TextInput(attrs={'class': 'form-control input'}), 'teaser': forms.Textarea(attrs={'class': 'form-control input'}), 'category': forms.Select(choices=categories, attrs={'class': 'form-control input'}), 'content': forms.Textarea(attrs={'class': 'form-control input'}), 'tags': forms.TextInput(attrs={'class': 'form-control input'}), 'is_published': forms.CheckboxInput(attrs={'class': 'form-control-input'}) } I found out if I created the field as an instance attrribute like this: category = forms.ModelChoiceField(queryset=ArticleCategory.objects.all().order_by('name')) Then I am able to use the form (with no errors raised). But I want to be able to specify the widget to use for that field, using the above declaration - is there anyway to do this - or am I restricted to simply declaring the field as an instance variable of the form? -
javascript passed string itself instead of index lenght, whats wrong with my code?
I am trying to pass the item id and item id index when a button is clicked. But the code is throwing different result. For your reference, I have attached expected result and obtained result. Any help will be highly appreciated if(localStorage.getItem('cart')==null){ var cart={}; } else{ cart = JSON.parse(localStorage.getItem('cart')); } $(document).on('click','.btn-cart',function(c){ c.preventDefault(); console.log("Add to Cart Button clicked"); var item_id = this.id.toString(); console.log(item_id); if(cart[item_id]!=undefined){ cart[item_id] =cart[item_id] + 1 } else{ cart[item_id]= 1 } console.log(cart); localStorage.setItem('cart',JSON.stringify(cart)); console.log(Object.keys(cart).length); }); I am expecting this: But it happened this: -
How to implement the function, I forgot the password and send an email to change it in Django
I would like to implement a "forgot password" function, and at that point an email would be sent so that she would be able to change her own password. I have not found documentation on this. Someone could show me a way. Thanks for listening! -
Cannot install psycopg2 with Github Actions due to distutils
I am trying to setup CI/CD for my Django project on Github. I deploy it to Heroku, which requires django-heroku package, which in turn depends on the psycopg2 package. When I'm trying to run this workflow: build-django: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.10' - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Run Django Tests run: | python manage.py test I get a huge error log which culminates in this: Copying psycopg2.egg-info to /opt/hostedtoolcache/Python/3.10.1/x64/lib/python3.10/site-packages/psycopg2-2.9.2-py3.10.egg-info running install_scripts Traceback (most recent call last): <...> ModuleNotFoundError: No module named 'distutils.command.bdist_wininst' Everything checks out on my machine. Why would I get this message? The requirements.txt file is setuptools~=44.1.1 wheel~=0.37.1 django~=3.2.9 djangorestframework~=3.12.4 pip~=20.3.4 pytz~=2021.3 sqlparse~=0.4.2 asgiref~=3.4.1 h11~=0.12.0 attrs~=21.2.0 trio~=0.19.0 outcome~=1.1.0 sniffio~=1.2.0 sortedcontainers~=2.4.0 cffi~=1.15.0 idna~=3.3 six~=1.16.0 cryptography~=36.0.1 certifi~=2021.10.8 urllib3~=1.26.7 selenium~=4.1.0 pycparser~=2.21 wsproto~=1.0.0 gunicorn~=20.1.0 django-heroku -
Get the Entire Tree Data using Tree query sets and Querysets from Django
I was trying to get the data from all the tree query set and query set using django ORM and Django MPTT. The Hierarchy of the DB tables is as follows: T1 (MPTT Model) T2 (MPTT Model) T3 (Django Model) T4 (Django Model) T5 (Django Model) T6 (Django Model) I have two MPTT models from which I can get the tree Query sets using logged in user filter, however, for retrieving all the other tables data, need to evaluate from other django models. Is it Possible to get all the data in single Queryset Evaluation set so that it will reduce the load on the Request? The Required Data : { key1: t1.value1, key2: t2.value2, key3: t3.value1, key4: [t4.values_list], key5: [t5.values_list], key6: t6.value3 }, { key1: t1.value1, key2: t2.value2, key3: t3.value1, key4: [t4.values_list], key5: [t5.values_list], key6: t6.value3 }, { key1: t1.value1, key2: t2.value2, key3: t3.value1, key4: [t4.values_list], key5: [t5.values_list], key6: t6.value3 } -
reCaptcha Enterprise in django forms without django-recaptcha
How can I implement new reCaptcha Enterprise within django on the form without using old django-recaptcha? -
test complex django method containing multiple ManyToMany field using pytest
my app : account_engine has following models: class Company(models.Model): name = models.CharField(max_length=200) class Location(models.Model): name = models.CharField(max_length=200) class Client(models.Model): name = models.CharField(max_length=200) class CustomPermission(models.Model): url_name = models.CharField(max_length=200) url = models.CharField(max_length=200) class CustomRole(models.Model): name = models.CharField(max_length=200) class RoleSpecificPermission(models.Model): role = models.ForeignKey(CustomRole, on_delete=models.CASCADE) permission = models.ManyToManyField(CustomPermission, blank=True) class GroupSpecificPermission(models.Model): group = models.ForeignKey(CustomGroup, on_delete=models.CASCADE) permission = models.ManyToManyField(CustomPermission, blank=True) class UserDetail(models.Model): mobile = models.CharField(max_length=10) user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) company = models.ForeignKey(Company, on_delete=models.CASCADE) location = models.ForeignKey(Location, on_delete=models.CASCADE) client = models.ManyToManyField(Client, blank=True) role = models.ManyToManyField(CustomRole, blank=True) group = models.ManyToManyField(CustomGroup, blank=True) special_permission = models.ManyToManyField(CustomPermission, blank=True) def get_all_permissions(self): # adding all permissions in set to remove duplicates final_set = set() role_level_prem = RoleSpecificPermission.objects.filter(role__in = list(self.role.all())) group_level_prem = GroupSpecificPermission.objects.filter(group__in = list(self.group.all())) for each_permission_set in role_level_prem: for each_permission in each_permission_set.permission.all(): final_set.add(each_permission) for each_permission_set in group_level_prem: for each_permission in each_permission_set.permission.all(): final_set.add(each_permission) for each_permission in self.special_permission.all(): final_set.add(each_permission) return list(final_set) I want to test the method get_all_permissions or model UserDetail class using pytest, pytest-django and mixer my test is as follows: class TestUserDetail: def test_method_get_all_permissions(self): roles = mixer.cycle(10).blend('account_engine.CustomRole') groups = mixer.cycle(10).blend('account_engine.CustomGroup') permissions = mixer.cycle(5).blend('account_engine.CustomPermission') special_permissions = mixer.cycle(5).blend('account_engine.CustomPermission') obj = mixer.blend('account_engine.UserDetail', role=mixer.sequence(*roles), group=mixer.sequence(*groups), permission=mixer.sequence(*permissions), special_permission=mixer.sequence(*special_permissions),) assert obj.get_all_permissions() == all_the_possible_permissions_for_which_i_need_your_inputs I'm just lost and not able to write test to verify if my method works … -
how to store a list of images in postgreSQL ArrayField (django rest)
I'm developing a project using django-rest for my backend and postgreSQL as my database. I'm using postgreSQL ArrayField in my Recipe model to store multiple images of different instructions. However when I send a POST request with a list of instructions images (instructions_image_list) I'am getting a weird error. I would really appreciate if anyone could help me solve it. thanks in advance. the error status_code 500 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte the model class Recipe(models.Model): id = models.UUIDField( primary_key=True, default=uuid.uuid4, editable=False, ) author = models.ForeignKey(get_user_model() , on_delete=models.CASCADE, null=True, related_name='author') photo_main = models.ImageField(upload_to='media/', blank=True) title = models.CharField(max_length=150) instructions_text_list = ArrayField(models.CharField(max_length=100, blank=True,null=True),default=list, blank=True ,null=True,size=15,) instructions_image_list = ArrayField(models.ImageField(upload_to='media/',blank=True, null=True),default=list, blank=True ,null=True, size=15,) the view class RecipeCreate(CreateAPIView): permission_classes = (permissions.IsAuthenticated, ) queryset = Recipe.objects.all() serializer_class = RecipeCreateSerializer def perform_create(self, serializer): '''save the the current logged in user as the author of the recipe''' serializer.save(author=self.request.user) -
Should csrf_token be used when posting form data to external website/ server?
Im new to Django. Im setting up a bank payment option (banklink) to my django website. As per the banks technical docs I have to post certain key/ value data to their endpoint. Im using an html form to POST the data and Im wondering if it is needed to add the {% csrf_token %} to the form that is being posted to the bank endpoint? If I post the data to my own endpoint then the form wont even work without the csrf token, but it seems to work when POSTing to an external endpoint. I assume if the external endpoint isnt using django then the added csrf_token value in POST data wouldnt mean anything to them anyways. Or are there any other considerations I should evaluate? Thank you -
Problems with the serializer validator do not allow data to be sent
In this question, I had a problem to pass the user to the serializer, which was solved, but now I face another problem that the serializer validator stops sending user information and thinks I am going to create a new user. views.py class ProfileView(generics.RetrieveAPIView): permission_classes = (IsAuthenticated,) serializer_class = UserSerializer def retrieve(self, request, *args, **kwargs): serializer = self.serializer_class(data=model_to_dict(request.user)) if serializer.is_valid(): return successful_response( messages=_('User Profile'), data=serializer.data ) return unsuccessful_response(errors=serializer.errors, status_code=status.HTTP_404_NOT_FOUND) My user model class: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(_('email address'), unique=True) ..... USERNAME_FIELD = 'email' .... errors: { "errors": { "email": [ "user with this email address already exists." ] } } -
How to integrate REST API with google auth android, so that only users that are signed up on the webapp log in via their google account on android?
I have used Django Allauth for user authentication in my django webapp(used google login). I have Rest APIs for the models in my project. Made Rest API for the "User" model too containing all the user info. Used Django Rest Framework for these. How do I use these integrate REST API with google auth android, so that only users that are signed up on the webapp can only log in via their google account on android? Please do not give vague, generic answers. Please give clear directions on what to do next. I am a beginner. -
SQL / Django views conditional cumulative sum of values
Pretty new to SQL and Django, I am trying to create a query to cumulate the value of column A (integer) if the value in column B (string) is different. This cumulation should be done based on date (column C). Here is the data: Column A Column B Column C 2.0 Rock 2020-08-08 3.0 Paper 2020-09-08 1.0 Rock 2021-09-09 25.0 Rock 2021-09-09 12.0 Rock 2021-10-10 5.0 Paper 2021-11-11 Based on this data, I would like to have a third column D, which will represent the cumulative value such as follow: Column A Column B Column C Column D 2.0 Rock 2020-08-08 2.0 3.0 Paper 2020-09-08 5.0 1.0 Rock 2021-09-09 4.0 25.0 Rock 2021-09-09 28.0 12.0 Rock 2021-10-10 15.0 5.0 Paper 2021-11-11 17.0 -
What is the correct way to make an API without a model behind it?
Suppose I want to make an API that is not supported by a model. Certain fields are entered with the request, some processing is applied, and then other fields are returned. The way I usually use it is to define a simple view that instantiates a serializer to do the validation. And then I return the fields directly from the view, for example with a JSONResponse. Is this the right way to do it? Does it make sense to use a serializer for validation? Is it better to use a form? Should the response be serialized? Is it correct to process the input data in the view? -
Make file dynamically with nginx_unit and django
I am trying to make with this csv_file = 'static/result.csv' with open(csv_file,'a') as f: It works on python manage runserver However on nginx_unit there comes error like this, PermissionError: [Errno 13] Permission denied: 'result.csv' static folder is 777 already lrwxrwxrwx 1 ubuntu ubuntu 38 Dec 28 19:32 static Why this permission error happens?? in my settings.py STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.join(config("PROJ_PATH"), 'staticroot') -
How to make asynchronious django requests?
I have few GET requests which I should send to 20 http addresses The problem that's the whole pack of requests is loading very slow, I want to be able to see the result for the finished requests already and then those requests that are slow or unresponsive should be appended to the final view result later. What is the simplest method of achiving this? I have already tried asyncio From this tutorial https://betterprogramming.pub/how-to-make-parallel-async-http-requests-in-python-d0bd74780b8a but it has nothing to do with django. Here is my code https://github.com/nuriyevn/django_mine/blob/master/monitor/views.py And I get SynchronousOnlyOperation at /monitor/ You cannot call this from an async context - use a thread or sync_to_async. http://194.0.52.235:9500/monitor/ -
Filtering Foreign key choices in Django serializer
I have a Django model that looks like the code below. At the moment, when I am using django rest framework to create a new menu instance, the dish column contains options created by all users on the platform. How should I go about filtering the dish column so it only has options created by the user? Should I be doing it in the views or serializer? Thank you for the response in advance. class Dish(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE, null=True, blank=True) title = models.CharField(max_length=280) description = models.CharField(max_length=280) image = models.ImageField(upload_to='static/images/post_image', default='static/images/post_image/default.jpg') def __str__(self): return f'{self.title}' def get_image_url(self, obj): return obj.image.url class Menu(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE, null=True, blank=True) title = models.CharField(max_length=280) description = models.CharField(max_length=280) dish = models.ManyToManyField(Dish) price = models.SmallIntegerField( validators=[MinValueValidator(1), MaxValueValidator(10000)], default=None) def __str__(self): return f'{self.title}' -
how to handle multiple records in graphql django update mutation
The below method updates single emp record , how to handle the same for multiple emp recs at the same time. { (id : 1, firstName : "John", lastName: "Snow"), (id : 2, firstName : "Tryrion", lastName: "Lannister") (id : 3, firstName : "Jammie", lastName: "Lannister") } I am new to django and graphql kindly help me with code and query for the same class UpdateEmp(graphene.Mutation): emp = graphene.Field(EmployeeType) class Arguments: id = graphene.Int(required=True) first_name = graphene.String() last_name = graphene.String() @login_required def mutate(self, info,**kwargs): emp = Employee.objects.get(id=kwargs.get('id')) emp.first_name=kwargs.get('first_name') emp.last_name=kwargs.get('last_name') emp.save() return UpdateEmp(emp=emp) graphql mutation{ uopdatemp(id : 1, firstName : "john", lastName: "Snow") { Employee{ id firstName, lastName } } }