Use django-import-export with class based views - python

I want to use django-import-export with class based views.
In the docs at https://django-import-export.readthedocs.org/en/latest/getting_started.html I see an example of exporting as csv
>>> dataset = BookResource().export()
>>> print dataset.csv
id,name,author,author_email,imported,published,price,categories
2,Some book,1,,0,2012-12-05,8.85,1
but which class based view should I use if I want to return an Excel file? Just View?

Jamgreen,
Based on implementation at https://github.com/bmihelac/django-import-export and considering a model "Country" as example with name and abreviation atributes:
First, define at the end of Country models file the Resource:
class CountryResource(resources.ModelResource):
class Meta:
model = Country
Then, implement the class based views:
class CountryExport(View):
def get(self, *args, **kwargs ):
dataset = CountryResource().export()
response = HttpResponse(dataset.csv, content_type="csv")
response['Content-Disposition'] = 'attachment; filename=filename.csv'
return response
class CountryImport(View):
model = Country
from_encoding = "utf-8"
#: import / export formats
DEFAULT_FORMATS = (
base_formats.CSV,
base_formats.XLS,
base_formats.TSV,
base_formats.ODS,
base_formats.JSON,
base_formats.YAML,
base_formats.HTML,
)
formats = DEFAULT_FORMATS
#: template for import view
import_template_name = 'Country/import.html'
resource_class = None
def get_import_formats(self):
"""
Returns available import formats.
"""
return [f for f in self.formats if f().can_import()]
def get_resource_class(self):
if not self.resource_class:
return modelresource_factory(self.model)
else:
return self.resource_class
def get_import_resource_class(self):
"""
Returns ResourceClass to use for import.
"""
return self.get_resource_class()
def get(self, *args, **kwargs ):
'''
Perform a dry_run of the import to make sure the import will not
result in errors. If there where no error, save the user
uploaded file to a local temp file that will be used by
'process_import' for the actual import.
'''
resource = self.get_import_resource_class()()
context = {}
import_formats = self.get_import_formats()
form = ImportForm(import_formats,
self.request.POST or None,
self.request.FILES or None)
if self.request.POST and form.is_valid():
input_format = import_formats[
int(form.cleaned_data['input_format'])
]()
import_file = form.cleaned_data['import_file']
# first always write the uploaded file to disk as it may be a
# memory file or else based on settings upload handlers
with tempfile.NamedTemporaryFile(delete=False) as uploaded_file:
for chunk in import_file.chunks():
uploaded_file.write(chunk)
# then read the file, using the proper format-specific mode
with open(uploaded_file.name,
input_format.get_read_mode()) as uploaded_import_file:
# warning, big files may exceed memory
data = uploaded_import_file.read()
if not input_format.is_binary() and self.from_encoding:
data = force_text(data, self.from_encoding)
dataset = input_format.create_dataset(data)
result = resource.import_data(dataset, dry_run=True,
raise_errors=False)
context['result'] = result
if not result.has_errors():
context['confirm_form'] = ConfirmImportForm(initial={
'import_file_name': os.path.basename(uploaded_file.name),
'input_format': form.cleaned_data['input_format'],
})
context['form'] = form
context['opts'] = self.model._meta
context['fields'] = [f.column_name for f in resource.get_fields()]
return TemplateResponse(self.request, [self.import_template_name], context)
def post(self, *args, **kwargs ):
'''
Perform a dry_run of the import to make sure the import will not
result in errors. If there where no error, save the user
uploaded file to a local temp file that will be used by
'process_import' for the actual import.
'''
resource = self.get_import_resource_class()()
context = {}
import_formats = self.get_import_formats()
form = ImportForm(import_formats,
self.request.POST or None,
self.request.FILES or None)
if self.request.POST and form.is_valid():
input_format = import_formats[
int(form.cleaned_data['input_format'])
]()
import_file = form.cleaned_data['import_file']
# first always write the uploaded file to disk as it may be a
# memory file or else based on settings upload handlers
with tempfile.NamedTemporaryFile(delete=False) as uploaded_file:
for chunk in import_file.chunks():
uploaded_file.write(chunk)
# then read the file, using the proper format-specific mode
with open(uploaded_file.name,
input_format.get_read_mode()) as uploaded_import_file:
# warning, big files may exceed memory
data = uploaded_import_file.read()
if not input_format.is_binary() and self.from_encoding:
data = force_text(data, self.from_encoding)
dataset = input_format.create_dataset(data)
result = resource.import_data(dataset, dry_run=True,
raise_errors=False)
context['result'] = result
if not result.has_errors():
context['confirm_form'] = ConfirmImportForm(initial={
'import_file_name': os.path.basename(uploaded_file.name),
'input_format': form.cleaned_data['input_format'],
})
context['form'] = form
context['opts'] = self.model._meta
context['fields'] = [f.column_name for f in resource.get_fields()]
return TemplateResponse(self.request, [self.import_template_name], context)
class CountryProcessImport(View):
model = Country
from_encoding = "utf-8"
#: import / export formats
DEFAULT_FORMATS = (
base_formats.CSV,
base_formats.XLS,
base_formats.TSV,
base_formats.ODS,
base_formats.JSON,
base_formats.YAML,
base_formats.HTML,
)
formats = DEFAULT_FORMATS
#: template for import view
import_template_name = 'Country/import.html'
resource_class = None
def get_import_formats(self):
"""
Returns available import formats.
"""
return [f for f in self.formats if f().can_import()]
def get_resource_class(self):
if not self.resource_class:
return modelresource_factory(self.model)
else:
return self.resource_class
def get_import_resource_class(self):
"""
Returns ResourceClass to use for import.
"""
return self.get_resource_class()
def post(self, *args, **kwargs ):
'''
Perform the actual import action (after the user has confirmed he
wishes to import)
'''
opts = self.model._meta
resource = self.get_import_resource_class()()
confirm_form = ConfirmImportForm(self.request.POST)
if confirm_form.is_valid():
import_formats = self.get_import_formats()
input_format = import_formats[
int(confirm_form.cleaned_data['input_format'])
]()
import_file_name = os.path.join(
tempfile.gettempdir(),
confirm_form.cleaned_data['import_file_name']
)
import_file = open(import_file_name, input_format.get_read_mode())
data = import_file.read()
if not input_format.is_binary() and self.from_encoding:
data = force_text(data, self.from_encoding)
dataset = input_format.create_dataset(data)
result = resource.import_data(dataset, dry_run=False,
raise_errors=True)
# Add imported objects to LogEntry
ADDITION = 1
CHANGE = 2
DELETION = 3
logentry_map = {
RowResult.IMPORT_TYPE_NEW: ADDITION,
RowResult.IMPORT_TYPE_UPDATE: CHANGE,
RowResult.IMPORT_TYPE_DELETE: DELETION,
}
content_type_id=ContentType.objects.get_for_model(self.model).pk
'''
for row in result:
LogEntry.objects.log_action(
user_id=request.user.pk,
content_type_id=content_type_id,
object_id=row.object_id,
object_repr=row.object_repr,
action_flag=logentry_map[row.import_type],
change_message="%s through import_export" % row.import_type,
)
'''
success_message = _('Import finished')
messages.success(self.request, success_message)
import_file.close()
url = reverse('%s_list' % (str(opts.app_label).lower()))
return HttpResponseRedirect(url)
the template import.html has the following code:
<h1>{% trans "Importar" %} {{ opts.app_label }}</h1>
{% if confirm_form %}
<form action="{% url "process_import" %}" method="POST">
{% csrf_token %}
{{ confirm_form.as_p }}
<p>
{% trans "Below is a preview of data to be imported. If you are satisfied with the results, click 'Confirm import'" %}
</p>
<div class="submit-row">
<input type="submit" class="btn" name="confirm" value="{% trans "Confirm import" %}">
</div>
</form>
{% else %}
<form action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form" enctype="multipart/form-data">
{% csrf_token %}
<p>
{% trans "This importer will import the following fields: " %}
{% for f in fields %}
{% if forloop.counter0 %}
,
{% endif %}
<tt>{{ f }}</tt>
{% endfor %}
</p>
<fieldset class="module aligned">
{% for field in form %}
<div class="form-row">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
{% if field.field.help_text %}
<p class="help">{{ field.field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
</fieldset>
<div class="submit-row">
<input type="submit" class="btn" value="{% trans "Submit" %}">
</div>
</form>
{% endif %}
{% if result %}
{% if result.has_errors %}
<h2>{% trans "Errors" %}</h2>
<ul>
{% for error in result.base_errors %}
<li>{{ error.error }}</li>
{% endfor %}
{% for line, errors in result.row_errors %}
{% for error in errors %}
<li>
{% trans "Line number" %}: {{ line }} - {{ error.error }}
<div class="traceback">{{ error.traceback|linebreaks }}</div>
</li>
{% endfor %}
{% endfor %}
</ul>
{% else %}
<h2>
{% trans "Preview" %}
</h2>
<table>
<thead>
<tr>
<th></th>
{% for field in fields %}
<th>{{ field }}</th>
{% endfor %}
</tr>
</thead>
{% for row in result.rows %}
<tr>
<td>
{% if row.import_type == 'new' %}
{% trans "New" %}
{% elif row.import_type == 'skip' %}
{% trans "Skipped" %}
{% elif row.import_type == 'delete' %}
{% trans "Delete" %}
{% elif row.import_type == 'update' %}
{% trans "Update" %}
{% endif %}
</td>
{% for field in row.diff %}
<td>
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
{% endif %}
and the urls.py should contain:
#export
url(r'export/$', login_required(CountryExport.as_view()), name='country_export'),
#import
url(r'import/$', login_required(CountryImport.as_view()), name='country_import'),
url(r'process_import/$', login_required(CountryProcessImport.as_view()), name='process_import'),

I've done the exact same thing today. I tried to make the view fairly generic and allow values to be passed through in the URL so it is a little more complex than simply returning an XLS file.
But anyway, this was my approach;
class PlaceResource(resources.ModelResource):
class Meta:
model = Place
class ExportPlacesView(View):
model = Place
def get(self, request, *args, **kwargs):
client_name = str(self.client).replace(' ', '_').lower()
if 'query' in kwargs:
if kwargs['query'] == u'complete':
complete_runners = Place.objects.filter(
~models.Q(runner__completed_on=None),
console=self.console).values_list(
'client',
flat=True).distinct()
dataset = PlaceResource().export(
Place.objects.filter(
console=self.console, client=self.client,
runner__in=complete_runners
)
)
filename = '{}_completed_runners'.format(client_name)
else:
dataset = PlaceResource().export(Place.objects.filter(
console=self.console, client=self.client,
))
filename = '{}_Runners'.format(client_name)
export_type = kwargs['format']
_dataset_methods = {
'csv': dataset.csv,
'xls': dataset.xls
}
response = HttpResponse(
_dataset_methods[export_type], content_type=export_type
)
response[
'Content-Disposition'] = 'attachment; filename={filename}.{ext}'.format(
filename=filename,
ext=export_type
)
return response

Related

current user.is_authenticated gives me a True value but it didn't work in base.html file

I'm trying to check if the user is logged in or not to change nav elements and to do more stuff and here is my code
And when I do this
print(attempted_account.is_authenticated) result => True
I think that the problem will be in base.html file so I hope you can help me.
routs.py:
#app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
attempted_account = Account.query.filter_by(email=form.email.data).first()
print(attempted_account.is_authenticated)
if attempted_account and attempted_account.check_password(attempted_password=form.password.data):
login_user(attempted_account)
flash('Success login ',category='success')
return redirect(url_for('page',id=1))
elif not attempted_account:
flash("The E-mail doesn't exist",category='danger')
else:
flash('Tha password is incorrect',category='danger')
return render_template('login.html',form=form)
base.html
<nav>
<ul>
<li><a class="home" href="{{ url_for('home') }}">Market</a></li>
<li>Go shopping</li>
</ul>
{% if current_user.is_authenticated %}
<ul>
<li>Logout</li>
</ul>
{% else %}
<ul>
<li>Sign up</li>
<li>Login</li>
</ul>
{% endif %}
</nav>
login.html
{% extends 'base.html' %}
{% block title %}
Login
{% endblock %}
{% block content %}
<div class="container">
<form method="POST" class="form-register" style="text-align: center;">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ form.email.label() }}
{{ form.email(class='form-control',placeholder='example#example.com') }}
<br>
{{ form.password.label() }}
{{ form.password(class='form-control',placeholder='Password') }}
<br>
{{ form.submit(class='btn btn-lg btn-block btn-primary') }}
</form>
</div>
{% endblock %}
Please note that I called my model Account not User
Edit:
Here is models.py:
from market import db
from market import bcrypt,login_manager
from flask_login import UserMixin
#login_manager.user_loader
def load_user(user_id):
return Account.query.filter_by(id=user_id)
class Account(db.Model,UserMixin):
id = db.Column(db.Integer,primary_key = True)
first_name = db.Column(db.String(length = 50),nullable = False)
last_name = db.Column(db.String(length = 50),nullable =False)
email = db.Column(db.String(length = 100),nullable =False,unique = True)
password_hashed = db.Column(db.String(length = 25),nullable = False)
#country = db.Column(db.String,nullable = False)
items = db.relationship('Item',backref = 'owner',lazy = True)
#property
def password(self):
return self.password
#password.setter
def password(self,plain_text_password):
self.password_hashed = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
def check_password(self,attempted_password):
return bcrypt.check_password_hash(self.password_hashed,attempted_password)
The issue is probably because you're returning a query result in the user_loader callback instead of an Account object.
Instead of returning Account.query.filter_by(id=user_id) try returning Account.query.filter_by(id=user_id).first().
Hope it works.

Django ModelForm Submit button does nothing

I have the following ModelForm, UpdateView and template but when I click on the 'Save' button and the 'Save and continue editing' button, nothing happens. I tried following the fix in this post but it didn't work. How do I post the data in these forms into their respective tables?
forms.py:
class ProductForm(SEOFormMixin, forms.ModelForm):
FIELD_FACTORIES = {
"text": _attr_text_field,
"richtext": _attr_textarea_field,
"integer": _attr_integer_field,
"boolean": _attr_boolean_field,
"float": _attr_float_field,
"date": _attr_date_field,
"datetime": _attr_datetime_field,
"option": _attr_option_field,
"multi_option": _attr_multi_option_field,
"entity": _attr_entity_field,
"numeric": _attr_numeric_field,
"file": _attr_file_field,
"image": _attr_image_field,
}
class Meta:
model = Product
fields = [
'title', 'upc', 'description', 'is_public', 'is_discountable', 'structure', 'slug', 'meta_title',
'meta_description']
widgets = {
'structure': forms.HiddenInput(),
'meta_description': forms.Textarea(attrs={'class': 'no-widget-init'})
}
def __init__(self, product_class, metal, data=None, parent=None, *args, **kwargs):
self.set_initial(product_class, metal, parent, kwargs)
super().__init__(data, *args, **kwargs)
if parent:
self.instance.parent = parent
# We need to set the correct product structures explicitly to pass
# attribute validation and child product validation. Note that
# those changes are not persisted.
self.instance.structure = Product.CHILD
self.instance.parent.structure = Product.PARENT
self.delete_non_child_fields()
else:
# Only set product class for non-child products
self.instance.product_class = product_class
self.instance.metal = metal
self.add_attribute_fields(product_class, metal, self.instance.is_parent)
if 'slug' in self.fields:
self.fields['slug'].required = False
self.fields['slug'].help_text = _('Leave blank to generate from product title')
if 'title' in self.fields:
self.fields['title'].widget = forms.TextInput(
attrs={'autocomplete': 'off'})
def set_initial(self, product_class, metal, parent, kwargs):
"""
Set initial data for the form. Sets the correct product structure
and fetches initial values for the dynamically constructed attribute
fields.
"""
if 'initial' not in kwargs:
kwargs['initial'] = {}
self.set_initial_attribute_values(product_class, metal, kwargs)
if parent:
kwargs['initial']['structure'] = Product.CHILD
def set_initial_attribute_values(self, product_class, metal, kwargs):
"""
Update the kwargs['initial'] value to have the initial values based on
the product instance's attributes
"""
instance = kwargs.get('instance')
if instance is None:
return
for attribute in product_class.attributes.all():
try:
value = instance.attribute_values.get(
attribute=attribute).value
except exceptions.ObjectDoesNotExist:
pass
else:
kwargs['initial']['attr_%s' % attribute.code] = value
for attribute in metal.product_attribute.all():
try:
value = instance.attribute_values.get(
attribute=attribute).value
except exceptions.ObjectDoesNotExist:
pass
else:
kwargs['initial']['attr_%s' % attribute.code] = value
def add_attribute_fields(self, product_class, metal, is_parent=False):
"""
For each attribute specified by the product class, this method
dynamically adds form fields to the product form.
"""
for attribute in product_class.attributes.all():
field = self.get_attribute_field(attribute)
if field:
self.fields['attr_%s' % attribute.code] = field
# Attributes are not required for a parent product
if is_parent:
self.fields['attr_%s' % attribute.code].required = False
for attribute in metal.product_attribute.all():
field = self.get_attribute_field(attribute)
if field:
self.fields['attr_%s' % attribute.code] = field
# Attributes are not required for a parent product
if is_parent:
self.fields['attr_%s' % attribute.code].required = False
def get_attribute_field(self, attribute):
"""
Gets the correct form field for a given attribute type.
"""
return self.FIELD_FACTORIES[attribute.type](attribute)
def delete_non_child_fields(self):
"""
Deletes any fields not needed for child products. Override this if
you want to e.g. keep the description field.
"""
for field_name in ['description', 'is_discountable']:
if field_name in self.fields:
del self.fields[field_name]
def _post_clean(self):
"""
Set attributes before ModelForm calls the product's clean method
(which it does in _post_clean), which in turn validates attributes.
"""
for attribute in self.instance.attr.get_all_attributes():
field_name = 'attr_%s' % attribute.code
# An empty text field won't show up in cleaned_data.
if field_name in self.cleaned_data:
value = self.cleaned_data[field_name]
setattr(self.instance.attr, attribute.code, value)
super()._post_clean()
views.py:
class ProductCreateUpdateView(PartnerProductFilterMixin, UpdateView):
template_name = 'oscar/dashboard/catalogue/product_update.html'
model = Product
context_object_name = 'product'
form_class = ProductForm
category_formset = ProductCategoryFormSet
image_formset = ProductImageFormSet
recommendations_formset = ProductRecommendationFormSet
stockrecord_formset = StockRecordFormSet
creating = False
parent = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.formsets = {'category_formset': self.category_formset,
'image_formset': self.image_formset,
'recommended_formset': self.recommendations_formset,
'stockrecord_formset': self.stockrecord_formset}
def dispatch(self, request, *args, **kwargs):
resp = super().dispatch(
request, *args, **kwargs)
return self.check_objects_or_redirect() or resp
def check_objects_or_redirect(self):
"""
Allows checking the objects fetched by get_object and redirect
if they don't satisfy our needs.
Is used to redirect when create a new variant and the specified
parent product can't actually be turned into a parent product.
"""
if self.creating and self.parent is not None:
is_valid, reason = self.parent.can_be_parent(give_reason=True)
if not is_valid:
messages.error(self.request, reason)
return redirect('dashboard:catalogue-product-list')
def get_queryset(self):
"""
Filter products that the user doesn't have permission to update
"""
return self.filter_queryset(Product.objects.all())
def get_object(self, queryset=None):
"""
This parts allows generic.UpdateView to handle creating products as
well. The only distinction between an UpdateView and a CreateView
is that self.object is None. We emulate this behavior.
This method is also responsible for setting self.product_class and
self.parent.
"""
self.creating = 'pk' not in self.kwargs
if self.creating:
# Specifying a parent product is only done when creating a child
# product.
parent_pk = self.kwargs.get('parent_pk')
if parent_pk is None:
self.parent = None
# A product class needs to be specified when creating a
# standalone product.
product_class_slug = self.kwargs.get('product_class_slug')
metal_slug = self.kwargs.get('metal_slug')
self.product_class = get_object_or_404(
ProductClass, slug=product_class_slug)
self.metal = get_object_or_404(
Metal, slug=metal_slug)
else:
self.parent = get_object_or_404(Product, pk=parent_pk)
self.product_class = self.parent.product_class
self.metal = self.parent.metal
return None # success
else:
product = super().get_object(queryset)
self.product_class = product.get_product_class()
self.parent = product.parent
self.metal = product.get_metal_type()
return product
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['product_class'] = self.product_class
ctx['metal'] = self.metal
ctx['parent'] = self.parent
ctx['title'] = self.get_page_title()
for ctx_name, formset_class in self.formsets.items():
if ctx_name not in ctx:
ctx[ctx_name] = formset_class(self.product_class,
self.request.user,
instance=self.object)
return ctx
def get_page_title(self):
if self.creating:
if self.parent is None:
return _('Create new %(product_class)s product') % {
'product_class': self.product_class.name}
else:
return _('Create new variant of %(parent_product)s') % {
'parent_product': self.parent.title}
else:
if self.object.title or not self.parent:
return self.object.title
else:
return _('Editing variant of %(parent_product)s') % {
'parent_product': self.parent.title}
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['product_class'] = self.product_class
kwargs['parent'] = self.parent
kwargs['metal'] = self.metal
return kwargs
def process_all_forms(self, form):
"""
Short-circuits the regular logic to have one place to have our
logic to check all forms
"""
# Need to create the product here because the inline forms need it
# can't use commit=False because ProductForm does not support it
if self.creating and form.is_valid():
self.object = form.save()
formsets = {}
for ctx_name, formset_class in self.formsets.items():
formsets[ctx_name] = formset_class(self.product_class,
self.request.user,
self.request.POST,
self.request.FILES,
instance=self.object)
is_valid = form.is_valid() and all([formset.is_valid()
for formset in formsets.values()])
cross_form_validation_result = self.clean(form, formsets)
if is_valid and cross_form_validation_result:
return self.forms_valid(form, formsets)
else:
return self.forms_invalid(form, formsets)
# form_valid and form_invalid are called depending on the validation result
# of just the product form and redisplay the form respectively return a
# redirect to the success URL. In both cases we need to check our formsets
# as well, so both methods do the same. process_all_forms then calls
# forms_valid or forms_invalid respectively, which do the redisplay or
# redirect.
form_valid = form_invalid = process_all_forms
def clean(self, form, formsets):
"""
Perform any cross-form/formset validation. If there are errors, attach
errors to a form or a form field so that they are displayed to the user
and return False. If everything is valid, return True. This method will
be called regardless of whether the individual forms are valid.
"""
return True
def forms_valid(self, form, formsets):
"""
Save all changes and display a success url.
When creating the first child product, this method also sets the new
parent's structure accordingly.
"""
if self.creating:
self.handle_adding_child(self.parent)
else:
# a just created product was already saved in process_all_forms()
self.object = form.save()
# Save formsets
for formset in formsets.values():
formset.save()
for idx, image in enumerate(self.object.images.all()):
image.display_order = idx
image.save()
return HttpResponseRedirect(self.get_success_url())
def handle_adding_child(self, parent):
"""
When creating the first child product, the parent product needs
to be implicitly converted from a standalone product to a
parent product.
"""
# ProductForm eagerly sets the future parent's structure to PARENT to
# pass validation, but it's not persisted in the database. We ensure
# it's persisted by calling save()
if parent is not None:
parent.structure = Product.PARENT
parent.save()
def forms_invalid(self, form, formsets):
# delete the temporary product again
if self.creating and self.object and self.object.pk is not None:
self.object.delete()
self.object = None
messages.error(self.request,
_("Your submitted data was not valid - please "
"correct the errors below"))
ctx = self.get_context_data(form=form, **formsets)
return self.render_to_response(ctx)
def get_url_with_querystring(self, url):
url_parts = [url]
if self.request.GET.urlencode():
url_parts += [self.request.GET.urlencode()]
return "?".join(url_parts)
def get_success_url(self):
"""
Renders a success message and redirects depending on the button:
- Standard case is pressing "Save"; redirects to the product list
- When "Save and continue" is pressed, we stay on the same page
- When "Create (another) child product" is pressed, it redirects
to a new product creation page
"""
msg = render_to_string(
'oscar/dashboard/catalogue/messages/product_saved.html',
{
'product': self.object,
'creating': self.creating,
'request': self.request
})
messages.success(self.request, msg, extra_tags="safe noicon")
action = self.request.POST.get('action')
if action == 'continue':
url = reverse(
'dashboard:catalogue-product', kwargs={"pk": self.object.id})
elif action == 'create-another-child' and self.parent:
url = reverse(
'dashboard:catalogue-product-create-child',
kwargs={'parent_pk': self.parent.pk})
elif action == 'create-child':
url = reverse(
'dashboard:catalogue-product-create-child',
kwargs={'parent_pk': self.object.pk})
else:
url = reverse('dashboard:catalogue-product-list')
return self.get_url_with_querystring(url)
urls.py:
urlpatterns += [
path(
'products/create/',
self.myapps_catalogue_views.ProductCreateRedirectView.as_view(),
name='catalogue-product-create'
),
path(
'products/create/<slug:product_class_slug>/<slug:metal_slug>/<slug:gemstone_slug>',
self.myapps_catalogue_views.ProductCreateUpdateView.as_view(),
name='catalogue-product-create'
),
]
template (selected parts):
<!--templates/oscar/dashboard/catalogue/product_update.html-->
{% extends 'oscar/dashboard/layout.html' %}
{% load form_tags %}
{% load i18n %}
{% block body_class %}{{ block.super }} create-page catalogue{% endblock %}
{% block title %}
{{ title }} | {% trans "Products" %} | {{ block.super }}
{% endblock %}
{% block breadcrumbs %}
#omitted due to space constraints
{% endblock %}
{% block headertext %}{{ title }}{% endblock %}
{% block dashboard_content %}
<form action="{% if request.GET.urlencode %}?{{ request.GET.urlencode }}{% endif %}" method="post" class="form-stacked wysiwyg fixed-actions" enctype="multipart/form-data" data-behaviour="tab-nav-errors" autocomplete="off">
{% csrf_token %}
{% if parent %}
<div class="row">
<div class="col-md-12">
<div class="alert alert-info">
{% url 'dashboard:catalogue-product' pk=parent.id as parent_url %}
{% blocktrans with title=parent.title %}
You are currently editing a product variant of
{{ title }}.
{% endblocktrans %}
</div>
</div>
</div>
{% endif %}
<div class="row">
{% block tab_nav %}
#omitted due to space constraints
{% endblock tab_nav %}
<div class="col-md-9">
<div class="tab-content">
{% block tab_content %}
{% block product_details %}
<div class="tab-pane active" id="product_details">
<div class="table-header">
<h3>{% trans "Product details" %}</h3>
</div>
<div class="card card-body product-details">
{% block product_details_content %}
<span class="error-block">{{ form.non_field_errors }}</span>
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
{% for field in form.primary_form_fields %}
{% if 'attr' not in field.id_for_label %}
{% include 'oscar/dashboard/partials/form_field.html' with field=field %}
{% endif %}
{% endfor %}
{% endblock product_details_content %}
</div>
</div>
{% endblock product_details %}
{% block product_categories %}
#omitted due to space constraints
{% endblock product_categories %}
{% block product_attributes %}
#omitted due to space constraints
{% endblock product_attributes %}
{% block product_images %}
#omitted due to space constraints
{% endblock product_images %}
{% block stockrecords %}
#omitted due to space constraints
{% endblock stockrecords %}
{% block child_products %}
#omitted due to space constraints
{% endblock child_products %}
{% block recommended_products %}
#omitted due to space constraints
{% endblock recommended_products %}
{% block seo %}
#omitted due to space constraints
{% endblock seo %}
{% block metal_attributes %}
<div class="tab-pane" id="metal_attributes">
{% block metal_attributes_content %}
<table class="table table-striped table-bordered">
<caption>
{% trans "Attributes" %}
<span class="badge badge-success">
{% trans "Metal Type:" %} {{ metal }}
</span>
</caption>
{% for field in form %}
{% if 'attr' in field.id_for_label %}
<tr>
<td>
{% include "oscar/dashboard/partials/form_field.html" %}
</td>
</tr>
{% endif %}
{% endfor %}
</table>
{% endblock metal_attributes_content %}
</div>
{% endblock metal_attributes %}
{% endblock tab_content %}
</div>
</div>
</div>
{% block fixed_actions_group %}
<div class="fixed-actions-group">
<div class="form-group">
<div class="float-right">
<a href="{% url 'dashboard:catalogue-product-list' %}">
{% trans "Cancel" %}
</a>
{% trans "or" %}
{% if parent %}
<button class="btn btn-secondary" name="action" type="submit" value="create-another-child" data-loading-text="{% trans 'Saving...' %}">
{% trans "Save and add another variant" %}
</button>
{% endif %}
<button class="btn btn-secondary" name="action" type="submit" value="continue" data-loading-text="{% trans 'Saving...' %}">
{% trans "Save and continue editing" %}
</button>
<button class="btn btn-primary" name="action" type="submit" value="save" data-loading-text="{% trans 'Saving...' %}">
{% trans "Save" %}
</button>
</div>
{% if product %}
<a class="btn btn-success" href="{{ product.get_absolute_url }}">{% trans "View on site" %}</a>
{% endif %}
</div>
</div>
{% endblock fixed_actions_group %}
</form>
{% endblock dashboard_content %}
Previews of the Template:
Just a suggestion: Take a look at the urls.py file, and change the paths order. Remember that Django evaluate them in order and take the first expression that match the pattern.
Also noticed that you're using request.GET.urlencode() in form action, but that will return only the URL GET params. Try to add the correct page URL there instead.

Django Form Wizard Render to Response on Step

I am working with the Django form wizard and i am trying to emulate the typical
render_to_response('index.html',{'name', name})
but without redirect and only in the steps.
So the idea would be i have a form wizard like this
TEMPLATES = {"0": "MyApp/checkout_template1.html",
"1": "MyApp/checkout_template2.html",
"2": "MyApp/checkout_template3.html",
}
class MyWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def get_context_data(self, form, **kwargs):
context = super(MyWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == '3':
data_1 = self.get_cleaned_data_for_step('1')
((HERE I NEED TO PASS THE data_1["first_name"] TO SHOW ON NEXT TEMPLATE))
print(data_1["first_name"])
return context
def done(self, form_list, **kwargs):
if self.request.method == 'POST':
process_form_data(form_list)
return render(self.request,'MyApp/congratulations.html')
then in templates id have something like this
{% extends "MyApp/base.html" %}
{% load staticfiles %}
{% load humanize %}
{% block head %}{{ wizard.form.media }}{% endblock %}
{% endblock %}
{% block body_block%}
<div class="confirmation-body">
<div class="confirmation-body-container">
<div class="form-title-container">
<h1>Information</h1>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="/checkout/" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{form}}
{% endfor %}
{% else %}
<div class="checkout-title">Information</div>
//////////////////////
{{ name }} #something like this that prints in the next step
//////////////////////
{{wizard.form.name_on_card}}
{% endif %}
</table>
<hr />
<div class="checkout-nav-controls">
<div>
{% if wizard.steps.prev %}
<button id="prev-step-button" name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">back</button>
{% endif %}
</div>
<input id="my-submit-button" type="submit" value="Next"/>
</div>
</form>
</div>
</div>
{% endblock %}
So just to reiterate the question for clairity. I want to:
get the data from the previous step (lets say first step and name)
in the second step pass that data to the corresponding html
template {{name}}
render that last step so i can see the name on
the front end
thanks in advance for your help
Maybe something like this whould do the trick, using get_form method:
def get_form(self, step=None, data=None, files=None):
form = super(MyWizard, self).get_form(step, data, files)
if step is None:
step = self.steps.current
if step == '3':
data_1 = self.get_cleaned_data_for_step('1')['first_name']
form.fields['first_form_field'].label = data1
return form
So in this way, you will have the field first_name from the first steps' form, as a label to the third step's form's first field
You can supply additional context variables by using the get_context_data() method of your WizardView subclass.
def get_context_data(self, form, **kwargs):
context = super(MyWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'my_step_name':
category=self.get_cleaned_data_for_step('category')['name']
context.update({'another_var': 'category'})
return context
get_context_data() docs

Django form field not displaying correct queryset values

The following code is from an multi-vendor ecommerce portal. We need to display different shipping methods according to the store(or vendor) on the checkout summary page.
However even though I get correct queryset while print i.e Store 1 has Test Rest of World Shipping method and Store 2 has UPC and DHL, the rendered form shows incorrect values -
#########################################################
class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect()
def label_from_instance(self, obj):
price_html = format_price(obj.price.gross, obj.price.currency)
label = mark_safe('%s %s' % (obj.shipping_method, price_html))
return label
class ShippingMethodForm(forms.Form):
def __init__(self, country_code, *args, **kwargs):
stores = kwargs.pop('stores')
super(ShippingMethodForm, self).__init__(*args, **kwargs)
for count, store in enumerate(stores, start=1):
method_field = ShippingCountryChoiceField(
queryset=ShippingMethodCountry.objects.select_related(
'shipping_method').order_by('price').filter(shipping_method__store=store),
label=pgettext_lazy('Shipping method form field label', 'Shipping method for %s' % store),
required=True)
if country_code:
queryset = method_field.queryset
method_field.queryset = queryset.unique_for_country_code(country_code)
if self.initial.get('method') is None:
method_field.initial = method_field.queryset.first()
method_field.empty_label = None
self.fields['method_%d' % count] = method_field
print [q.queryset for q in self.fields.values()]
###################################################
#load_checkout
#validate_voucher
#validate_cart
#validate_is_shipping_required
#validate_shipping_address
#add_voucher_form
def shipping_method_view(request, checkout):
country_code = checkout.shipping_address.country.code
stores = checkout.cart.lines.values_list('variant__product__store', flat=True)
stores = Store.objects.filter(id__in=stores)
print checkout.shipping_method
shipping_method_form = ShippingMethodForm(
country_code, request.POST or None, initial={'method': checkout.shipping_method},
stores=stores)
if shipping_method_form.is_valid():
for count, store in enumerate(stores):
checkout.shipping_method[store] = shipping_method_form.cleaned_data['method_%s' % count]
return redirect('checkout:summary')
print [q.queryset for q in shipping_method_form.fields.values()]
return TemplateResponse(request, 'checkout/shipping_method.html', context={
'shipping_method_form': shipping_method_form, 'checkout': checkout})
##############################################################
{% extends "checkout/details.html" %}
{% load i18n %}
{% load gross from prices_i18n %}
{% load bootstrap_form from bootstrap3 %}
{% block forms %}
<h3>{% trans "Shipping address" context "Checkout shipping address title" %}</h3>
{% include "userprofile/snippets/address-short.html" with address=checkout.shipping_address only %}
<p>{% trans "Select other address" %}</p>
<hr>
<form method="post" novalidate>
{% csrf_token %}
{% bootstrap_form shipping_method_form show_label=True %}
<p class="text-md-right">
<button type="submit" class="btn primary">
{% trans "Continue" context "Checkout shipping method primary action" %}
</button>
</p>
</form>
{% endblock %}
I believe the problem is that you are instantiating the widget in the field definition. This could cause state to be shared between different fields. Try changing it to:
class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect
...

Trouble accessing dictionary values inside a Django template

Here's my view function:
def detail(request, movie_id):
movie = Movie.objects.get(id = movie_id)
movieReleases = Release.objects.filter(movie = movie)
reviews = Review.objects.filter(movie = movie)
initRating = 0
if request.user.is_authenticated():
user = Account.objects.get(user = request.user.id)
try:
myreview = Review.objects.get(movie = movie, user = user)
initRating = myreview.rating
except:
pass
releaseContext = dict()
for release in movieReleases:
if release.isReleased():
releaseContext[release.id] = {'buttonURL' : reverse('movies.views.add', args=(release.id,)), 'buttonText' : 'Add to cart'}
elif MovieReleaseReminder.objects.filter(account=user, movie=release).exists():
releaseContext[release.id] = {'buttonURL' : reverse('movies.views.unsubscribeFromReleaseReminder', args=(release.id,)), 'buttonText' : 'Unsubscribe from release reminder'}
else:
releaseContext[release.id] = {'buttonURL' : reverse('movies.views.subscribeForReleaseReminder', args=(release.id,)), 'buttonText' : 'Subscribe to release reminder'}
if MovieUpdateSubscription.objects.filter(account=user, movie=movie).exists():
movieSubscribeURL = reverse('movies.views.unsubscribeFromUpdates', args=(movie.id,))
movieSubscribeButtonText = 'Unsubscribe from updates'
else:
movieSubscribeURL = reverse('movies.views.subscribeForUpdates', args=(movie.id,))
movieSubscribeButtonText = 'Subscribe for updates'
else:
releaseContext = None
movieSubscribeURL = None
movieSubscribeButtonText = None
averageRating = calculateAverageRating(movie)
t = loader.get_template('movies/detail.html')
c = RequestContext(request, {'movie' : movie,
'movieReleases' : movieReleases,
'releaseContext' : releaseContext,
'reviews': reviews,
'averageRating': averageRating,
'initRating': initRating,
'movieSubscribeURL' : movieSubscribeURL,
'movieSubscribeButtonText' : movieSubscribeButtonText})
return HttpResponse(t.render(c))
Here's the template code in question:
{% if movieReleases%}
<table border="1px">
{% for release in movieReleases %}
<tr>
<td>{{release.format}}</td>
<td>{{release.region}}</td>
<td>{{release.price}}</td>
{% if user.is_authenticated %}
{% with release_id=release.id %}
<td><form action="{{ releaseContext.release_id.buttonURL }}" method="post">{% csrf_token %}<input type="submit" value="{{ releaseContext.release_id.buttonText }}" /></form></td>
{% endwith %}
{% endif %}
</tr>
{% endfor %}
</table>
{% endif %}
The problem is that both {{ releaseContext.release_id.buttonURL }} and {{ releaseContext.release_id.buttonText }} are evaluating to the empty string when the template is rendered, even though I've checked in the debugger that releaseContext has the appropriate data. What am I missing?
Since releaseContext is a dictionary, attribute lookup (x.y) is not what you require, but element lookup (x[y]).
So instead of:
{{ releaseContext.release_id.buttonURL }}
use
{{ releaseContext['release_id']['buttonURL'] }}
Try getting the value out of the dictionary before handing it to the template:
{% display = releaseContext['release_id']['buttonURL'] %}
{{ display }}

Categories

Resources