How to fix trouble with urls better django - python

I have a section residential interiors on the site, so the typical url looks like this https://caparolcenterspb.ru/decision/livingrooms/kitchen/provans/ (room and style)
However different rooms may have the same styles and when searching for styles in views.py it may output several styles and an error
views.py
selected_room = Room.objects.get(slug=rmslg)
style = Style.objects.get(slug=stslg)
When you try to replace slug with different styles depending on the room(for example, provans_kitchen), an error occurs in the template(just put provans by default will not work)
residentialInteriors.html
{% for room in all_rooms %}
<li class="menu__item menu__item_interiors">{{ room.name }}</li>
{% endfor %}
I have 2 solution ideas(preferably 2):
either change stslg in template by default to 'provans_' + str(room. slug), but this line does not work(especially not the fact that provans will be everywhere)
either search for style in views.py not only for stslg, but also for rmslg, but for this in the style model, you need to create a room field inherited from the room model, which also does not work for me, since Room is declared further than Style
models.py
class Style(models.Model):
name = models.CharField(max_length=30)
full_name = models.CharField(max_length=50)
slug = models.SlugField()
img = models.ImageField(upload_to='img/')
walls = models.TextField()
floor = models.TextField()
roof = models.TextField()
def __str__(self):
return self.full_name
class Meta:
verbose_name = 'Стили'
verbose_name_plural = 'Стили'
class Room(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField()
styles = models.ManyToManyField(Style)
def __str__(self):
return self.name
class Meta:
verbose_name = 'Комнаты'
verbose_name_plural = 'Комнаты'

Sounds like you want to use the relation field that you already have on Room
to only find styles associated with the given room?
selected_room = Room.objects.get(slug=rmslg)
style = selected_room.styles.get(slug=stslg)

Related

Displaying objects by categories in Django

I'm building an ecommerce app and I'd like to make a section that shows some featured items by category. I'm using three sliders that display those items; Each slider is a featured category and each item in the slider is a featured item.
The problem is that I can't figure out how to assign the item to the proper slider. For example: I want to assign a JeansJacket to "Clothes and accesories" and display it. I tried this:
{% for cat in categories %}
<h1>{{ cat.cat_name }}</h1>
<!--(carousel code in between)-->
<div class="carousel-inner" role="listbox">
{% for item in featured_items %}
{% if item.Categoría in cat.cat_name %}
{{ item }}
{% endif %}
This is a simplified version of what I have, without the rest of the content. I just can't figure out how to iterate though the featured items and display them in the corresponding category.
Edit: This is in models.py:
class Categorías(models.Model):
cat_name = models.CharField(max_length=30)
Destacado = models.BooleanField()
class Meta:
ordering = ('cat_name',)
verbose_name = 'Categoría'
verbose_name_plural = 'Categorías'
def __str__(self):
return self.cat_name
class publicaciones(models.Model):
Título = models.CharField(max_length=30)
Descripción = models.TextField(max_length=200)
Precio = models.FloatField()
Fotos = models.ImageField()
Categoría = models.ForeignKey(Categorías, on_delete=models.CASCADE)
Promocionado = models.BooleanField()
class Meta:
verbose_name = 'Publicación'
verbose_name_plural = 'Publicaciones'
def __str__(self):
return self.Título
You can use prefetch_related and Prefetch with a custom query to get all related articles for each category.
categories = Categorías.objects.prefetch_related(Prefetch(
'publicaciones_set',
queryset=publicaciones.objects.filter(Promocionado=True),
to_attr='featured_items'
))
Now you can loop over each category and then loop over this prefetch. You don't need to create a separate featured_items queryset
for category in categories:
for featured_item in category.featured_items:
...
You can use apply this pattern to your template

Django fields in form

I am a beginner in Django, hence this might be a simple issue. But I'm not able to get past this successfully.
This is my models.py
class Category(models.Model):
name = models.CharField(max_length=128)
abbr = models.CharField(max_length=5)
def __unicode__(self):
return self.name
class Fabric(models.Model):
name = models.CharField(max_length=128)
abbr = models.CharField(max_length=5)
def __unicode__(self):
return self.name
class Manufacturer(models.Model):
name = models.CharField(max_length=128)
location = models.CharField(max_length=128)
name_abbr = models.CharField(max_length=5, default=None)
loc_abbr = models.CharField(max_length=5, default=None)
def __unicode__(self):
return self.name
class Images(models.Model):
design_id = models.CharField(max_length=128)
file = models.ImageField(upload_to='images')
cost_price = models.FloatField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
fabric = models.ForeignKey(Fabric, on_delete=models.CASCADE)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
selling_price = models.FloatField()
aliveness = models.IntegerField()
date_added = models.DateTimeField(default=datetime.datetime.now)
set_cat = models.IntegerField()
set_cat_no = models.IntegerField()
set_cat_name = models.CharField(max_length=50, blank=True)
I'm building an apparel management database system which contains cloth designs.
My forms.py is
class ImagesForm(forms.ModelForm):
class Meta:
model = Images
fields = ('file','cost_price','set_cat_no','set_cat_name',)
My views.py
#login_required
def uploadphoto(request):
context = RequestContext(request)
context_dict = {}
if request.method == 'POST':
form = ImagesForm(request.POST,request.FILES)
if form.is_valid():
image = form.save(commit=False)
image.save()
return render_to_response(request,'cms/upload-photo.html', {'upload_image': form})
else:
print form.errors
else:
form = ImagesForm()
context_dict = {'upload_image': form}
return render_to_response('cms/upload-photo.html',context_dict, context)
My upload-photo.html is
{% block main %}
<form id="upload_form" method="post" action="/zoomtail/upload-photo/" enctype="multipart/form-data">
{% csrf_token %}
{{ upload_image }}
</form>
{% endblock %}
The problem here is when I goto /upload-photo/ I don't see the drop-downs to the foreign keys to categories, fabric and manufacturers. I have read that it should be automatically generated but I don't see any.
And selling_price has to be calculated based on the given percentage increase from the cost_price which has to be entered in the form. How do I do this? And aliveness of an apparel has to be set by default as 1. How to do this?
set-cat field of an apparel is 1 if it belongs to a set and 2 if it belongs to a catalogue. How do I get a radio button asking whether set or catalogue and that to be captured as an integer in the database?
And design-id field of an apparel has to be a string which contains abbreviations of category, fabric, manufacturer, etc etc all the fields it belongs to. How do I do this dynamically?
I know, this is a very long question but I'm a newbie and these are literally giving me headache since days. I shall be very thankful to those who answer this.
I believe the issue with the dropdown is that you've excluded the fields from ImageForm. You have:
fields = ('file','cost_price','set_cat_no','set_cat_name',)
but should have:
fields = ('file','cost_price','set_cat_no','set_cat_name', 'category', 'fabric', 'manufacturer,)`
if that doesn't work, are there any options in your database for Categories, Fabric, and Manufacturer? If your tables are empty, the dropdown will be empty. If there are values in the database, is there HTML being generated but the label value is blank (i.e. <option>{this is blank}</option>)? In django, you can override the __str__ function to specify how the dropdown options get labeled
Override __str__ as follows:
class Category(models.Model):
name = models.CharField(max_length=128)
abbr = models.CharField(max_length=5)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
You can compute the value of selling_price and any other computed value in the block if request.method == 'POST'.
Example:
def uploadphoto(request):
context = RequestContext(request)
context_dict = {}
if request.method == 'POST':
form = ImagesForm(request.POST,request.FILES)
#- Calculate value(s) here -#
if form.is_valid():
image = form.save(commit=False)
image.save()`
Please see this post here for using radio buttons
You would do this in the same place as #2 above

Hiding foreign key fields from ModelForms in Django Template

Essence in the following.
I have a few different Magazine. And AutoForm from template shows all Articles and all Authors for possible selection (not only from current Magazine). How to restrict this choice of author a for article only from the current magazine? Can I do this using only the Template? If not, then how?
models.py
class Magazine(models.Model):
Name = models.CharField(max_length=200)
class Article(models.Model):
Name = models.CharField(max_length=200)
Magazine = models.ForeignKey(Magazine)
class Author(models.Model):
Name = models.CharField(max_length=200)
Magazine = models.ForeignKey(Magazine)
class Sets(models.Model):
Name = models.CharField(max_length=200)
Magazine = models.ForeignKey(Magazine)
Aut = models.ManyToManyField(Author, null=True, blank=True)
Set = models.ForeignKey(Article, null=True, blank=True)
forms.py:
class MagazineForm(ModelForm):
class Meta:
model = Magazine
fields = {'Name'}
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = {'Name'}
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = {'Name'}
class SetsForm(ModelForm):
class Meta:
model = Sets
fields = {'Name', 'Set', 'Aut'}
views.py
def building_details(request, magazine_id):
sets_form = SetsForm
args = {}
args.update(csrf(request))
args['magazine'] = Magazine.objects.get(id=magazine_id)
args['article'] = Article.objects.filter(Magazine=magazine_id)
args['author'] = Author.objects.filter(Magazine=magazine_id)
args['sets'] = Sets.objects.filter(Magazine=magazine_id)
args['setform'] = sets_form
return render_to_response('building.html', args)
Template
<form action='/add/{{ magazine.id }}/' method='post'>
{% csrf_token %}
{{ setform }}
<input type='submit' class='button' value="Добавить">
</form>
</div>
Maybe somewhere in this entry there are errors (I briefly edited, removing not playing a role here, essentially). In General I have everything working right, except that all objects are displayed, instead of only having the current.
Sounds like you are trying to restrict the options in the ForeignKey selector based on some criteria.
This means you need to override some of the details in the forms __init__ method:
class SetsForm(ModelForm):
class Meta:
model = Sets
fields = {'Name', 'Set', 'Aut'}
def __init__(self, *args, **kwargs):
articles = Article.objects.filter(Magazine=kwargs.pop('magazine_id'))
super(SetsForm, self).__init__(*args, **kwargs)
self.fields['articles'] = forms.ModelChoiceField(
queryset=articles ,
label="Articles",
)
Then call your form like so:
sets_form = SetsForm(magazine_id=magazine_id)
To get authors of articles only in the current magazine:
articles = Article.objects.filter(Magazine=magazine_id)
sets = Sets.objects.filter(Magazine=magazine_id, Set__in=articles)
import itertools
# build a flat list of all Authors in these sets (and call `set` to avoid duplicates)
authors = set(itertools.chain(*[s.Aut.all() for s in sets]))
This would be easier if there were a direct foreign key on Author to Article.
Also, you should follow Django convention by using lowercase field names. This will avoid confusion between fields and models, which use uppercase.

Django template sort by 'unrelated' model's field

I'm trying to sort related items in a template by a field in a model three ForeignKey relationships away. I'm assembling the data for the template in the view as proposed in another StackOverflow answer:
Sorting related items in a Django template
As far as I can tell, I copied the code from this as-is except for I had to change variable names. It doesn't throw an error, it just displays no list items in the HTML unordered list.
# checkout.html
{% for item in cart_items %}
<tr>
<td class="left">
{{ item.name }}
<ul>
{% for part in part_list %}
<li>{{ part.name }}
{% endfor %}
</ul></td>
</tr>
{% endfor %}
And the view...
# views.py
def checkout(request):
cart_items = get_cart_items(request)
itemCollection = []
for item in cart_items:
item.part_list = item.product.buildpart.all().order_by('product__family__type')
itemCollection.append(item)
return render(request, 'checkout.html', locals())
And the get_cart_items function:
# cart.py
def get_cart_items(request):
""" return all items from the current user's cart """
return CartItem.objects.filter(cart_id=get_cart_id(request))
As I said, the template and view are pretty much copies of the solution presented in the aforementioned StackOverflow article. One thing I thought was curious was that itemCollection[] from the view is never referenced in the template.
I believe the order_by clause ('product__family__type') is right only because it doesn't generate an error. But in case that is the problem or a part of it here is the chain of models I am attempting to navigate in that order_by clause:
We start from the shopping cart model (CartItem):
class Item(models.Model):
cart_id = models.CharField(max_length=50)
quantity = models.IntegerField(default=1)
product = models.ForeignKey(PartModel, unique=False)
class Meta:
abstract = True
class CartItem(Item):
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['date_added']
verbose_name = "Cart Item"
Through the 'product' field we get to the model holding our inventory and its self-referential BuildPart ManyToMany model:
class PartModel(models.Model):
family = models.ForeignKey(PartFamily)
name = models.CharField("Model Name", max_length=50, unique=True)
buildpart = models.ManyToManyField('self', through='BuildPart',
symmetrical=False, related_name='+')
class Build(models.Model):
build = models.ForeignKey(PartModel, related_name='+')
part = models.ForeignKey(PartModel, related_name='+')
quantity = models.PositiveSmallIntegerField(default=1)
class Meta:
abstract = True
unique_together = ('build', 'part')
def __unicode__(self):
return self.build.name + ' with ' + str(self.quantity) + ' * ' + \
self.part.family.make.name + ' ' + self.part.name
class BuildPart(Build):
pass
class Meta:
verbose_name = "Build Part"
From there we follow the 'family' field to the PartFamily model:
class PartFamily(models.Model):
make = models.ForeignKey(PartMake)
type = models.ForeignKey(PartType)
name = models.CharField("Family Name", max_length=30,
unique=True)
slug = models.SlugField(unique=True)
And lastly, we get to the model with the 'order' field, the one we wish to sort the related items by, PartType:
class PartType(models.Model):
name = models.CharField("Part Type", max_length=30, unique=True)
slug = models.SlugField(unique=True)
order = models.PositiveSmallIntegerField()
description = models.TextField(blank=True, null=True)
To recap, how do I get the shopping cart products' related items, and sort them by the 'order' field in the PartType model?
You have two errors, both in the template.
Firstly, you've put your items with the sorted relationship in a list called itemCollection, but in the template you're iterating over cart_item instead. This is a very good example of why you should be explicit about what variables you pass to the template, rather than relying on locals().
Secondly, you then iterate over part_list without defining it. You mean item.part_list.

Django showing in a template a ManytoManyField that is into a Dictionary

The thing is quite simple but i don't know how to do it.
I got the next models.py
class ElementoDeRed(models.Model):
IP_Address = models.CharField(max_length=200, primary_key= True)
MAC_Address = models.CharField(max_length=200)
offset_bytes = models.CharField(max_length=200)
transfered_bytes = models.CharField(max_length=200)
time = models.CharField(max_length=200)
previous_bytes = models.CharField(max_length=200)
previous_previous_bytes = models.CharField(max_length=200)
class Meta:
verbose_name = 'Dispositivos en mi nube'
verbose_name_plural = 'Dispositivos en mi nube'
def __unicode__ (self):
return smart_unicode(self.IP_Address)
class Register(models.Model):
user = models.ForeignKey(User)
network_element = models.ManyToManyField(ElementoDeRed)
registered_date = models.DateTimeField(null=True,blank=True)
def __unicode__ (self):
#return smart_unicode(self.network_element)
return smart_unicode("%s: %s" % (self.user,self.network_element.all()[0]))
As you can see, Register is using a ElementoDeRed element to storage on itself.
The thing is that i want to show in my template something like :
"The user <'user'> has the next device configured with this IP : <'IP_Address'>, Mac: <'Mac_Address'> ...."
What I am rendering on the template is a var called "dict_username_registers", is a dictionary witch key value is the username and the items asocciate to it are the "registers" fields.
But im getting something like :
david: [<Register: david: 10.0.0.3>, <Register: david: 10.0.0.1>] , Alice: <Register: Alice: 10.0.0.2>]
How can i access to that field in HTML????
Thank you! Let me know if u need something else!
you can iterate over the elements of dict_username_registers like this
{% for register in dict_username_registers %}
accessing date {{register.registered_date}}
{% endfor %}
you car read more about those tags here

Categories

Resources