django-views

Django form with m2m relationship not saving

Django form with m2m relationship not saving Question: I have a form where I want request.user to populate as little as possible and rely on the views to populate other fields automatically. As a result, some of these fields are not rendered on the form. The code in my view seems to work fine for …

Total answers: 1

Django media image not showing up in template but can acces it via …/media/thumbnail/image.png

Django media image not showing up in template but can acces it via …/media/thumbnail/image.png Question: Altough context does render in html, i can use other variables but cant accsess the image. My code is: html: {% for a in Manga %} <div class="manga_container"> <div class="manga_image"> <p>{{a.manga_image}}</p> <img src="{{ x.manga_image.url }}" alt=""> </div> </div> {%endfor%} model: …

Total answers: 1

(Django CBV) Need object be attached to a user with CBV

(Django CBV) Need object be attached to a user with CBV Question: I implemented this functionality with using FBV, but when I’m trying to use CBV, Objects were created with empty user field. views.py class BlockCreate(CreateView): model = TrainingBlock template_name = ‘training_room/create_block.html’ form_class = BlockForm success_url = reverse_lazy(‘gym’) def set_user(self, form): form.instance.user = self.request.user return …

Total answers: 1

how to call a function as a context in django

how to call a function as a context in django Question: class User(AbstractUser): GENDER_STATUS = ( (‘M’, ‘Male’), (‘F’, ‘Female’) ) address = models.TextField(null=True, blank=True) age = models.PositiveIntegerField(null=True, blank=True) description = models.TextField(null=True, blank=True) gender = models.CharField(max_length=1, choices=GENDER_STATUS, null=True, blank=True) phone = models.CharField(max_length=15, null=True, blank=True) def get_full_name(self): return f'{self.first_name} {self.last_name}’ I declare a function get_full_name and …

Total answers: 2

Reverse for 'update-project' with arguments '('',)' not found. 1 pattern(s) tried: ['update-project/(?P<pk>[0-9]+)/Z']

Reverse for 'update-project' with arguments '('',)' not found. 1 pattern(s) tried: ['update\-project/(?P<pk>[0-9]+)/\Z'] Question: Trying to create an update function for a Project Model in Django but i’ve run into a problem. Here’s what i have so far update view function @login_required def updateProject(request, pk): project = Project.objects.get(id=pk) form = ProjectForm(instance=project) if request.method == ‘POST’: project.name …

Total answers: 1

How Can I Display Multiple Models In a Django ListView?

How Can I Display Multiple Models In a Django ListView? Question: I am trying to display several models via a ListView. After some research…I have determined that I can do something like… class MultiModelListView(LoginRequiredMixin,ListView): model = MultiModel context_object_name = ‘thing_list’ template_name = ‘view_my_list.html’ paginate_by = 15 def get_context_data(self, **kwargs): context = super(MultiModelListView, self).get_context_data(**kwargs) list1 = …

Total answers: 1

Django error: Cannot assign "''": "" must be a "" instance

Django error: Cannot assign "''": "" must be a "" instance Question: I am getting this error when adding a pair. Cannot assign "’1’": "Pair.exchange" must be a "Exchange" instance. models.py: class Exchange(models.Model): name = models.CharField(max_length=25) def __str__(self) -> str: return f'{self.name}’ class Pair(models.Model): symbol = models.CharField(max_length=20) ask = models.FloatField() bid = models.FloatField() exchange = …

Total answers: 2

AttributeError Exception: Serializer has no attribute request in DRF

AttributeError Exception: Serializer has no attribute request in DRF Question: I have written following code in serializer where I am validating data: class MySerializer(serializers.ModelSerializer): class Meta: model = models.MyClass fields = "__all__" def validate(self, data): role = data["role"] roles = models.Role.objects.filter( –>(exception) organization=self.request.user.organization ) if role not in roles: raise serializers.ValidationError("Invlid role selected") return data …

Total answers: 1

AttributeError at /addtowatchlist/5: 'QuerySet' object has no attribute 'listings'

AttributeError at /addtowatchlist/5: 'QuerySet' object has no attribute 'listings' Question: Within my app, I’m allowing any given user to add an auction listing to their watch list, but no matter what I have tried, it does not seem to work and I keep getting the error in the title. I am simply trying to access …

Total answers: 2

How to set field values dynamically based on other fields in django

How to set field values dynamically based on other fields in django Question: I have two models in Django. class Product(models.Model): description = models.CharField(‘Description’, max_length=200) price = models.FloatField() class Sell(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) price = models.FloatField() def save(self, *args, **kwargs): self.price = self.product.price super(Sell, self).save(*args, **kwargs) I want to copy dynamically Product.price value to …

Total answers: 1