first hit to addtocartform

This commit is contained in:
Ivan Hörler 2018-02-01 17:02:08 +01:00
parent 9c23d667c8
commit eb77635920
4 changed files with 66 additions and 29 deletions

View File

@ -51,3 +51,11 @@ class PictureForm(forms.ModelForm):
class Meta: class Meta:
model = Picture model = Picture
fields = ['name', 'article', 'image'] fields = ['name', 'article', 'image']
class AddToCartForm(forms.Form):
amount = forms.IntegerField(
label='Amount in piece.',
help_text="Enter a Value between 1 and 99.",
initial=1)
print("in AddToCartForm() printing")

View File

@ -6,6 +6,11 @@
<p><b>Stock:</b> {{ article.stock }}</p> <p><b>Stock:</b> {{ article.stock }}</p>
<p><b>Status:</b> {{ article.status }}</p> <p><b>Status:</b> {{ article.status }}</p>
<p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p> <p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p>
<form id="amount" action="" method="POST" novalidate>
{{ amount.as_p }}
<input type="submit" value="Add to Cart" />
{% csrf_token %}
</form>
{% for picture in picture_list %} {% for picture in picture_list %}
<p><img src="{{ MEDIA_URL }}{{ picture.image }}" width="200" /></p> <p><img src="{{ MEDIA_URL }}{{ picture.image }}" width="200" /></p>
{% endfor %} {% endfor %}

View File

@ -19,9 +19,9 @@
<a href="{% url 'login' %}">LOGIN</a> <a href="{% url 'login' %}">LOGIN</a>
</li> </li>
{% endif %} {% endif %}
<li> <li class="dropdown">
{% if article_view %} {% if article_view %}
<form id="currency" name="currency" action="" method="POST" novalidate> <form id="currency" action="" method="POST" novalidate>
{{ currencies_form.as_ul }} {{ currencies_form.as_ul }}
<li> <li>
<input type="submit" value="Select"> <input type="submit" value="Select">

View File

@ -5,7 +5,7 @@ from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm
from webshop.models import (Article, Category, ArticleStatus, Person, from webshop.models import (Article, Category, ArticleStatus, Person,
City, Picture) City, Picture)
from webshop.forms import RegistrationForm from webshop.forms import RegistrationForm, AddToCartForm
from currencies.models import ExchangeRate, ExchangeRate_name from currencies.models import ExchangeRate, ExchangeRate_name
from currencies.forms import CurrenciesForm from currencies.forms import CurrenciesForm
@ -33,7 +33,7 @@ def index(request):
articles = Article.objects.all().exclude(status=get_hidden_status_id()) articles = Article.objects.all().exclude(status=get_hidden_status_id())
articles_list = list(articles) articles_list = list(articles)
currencies_form = CurrenciesForm currencies_form = CurrenciesForm
rate=ExchangeRate rate = ExchangeRate
article_view = True article_view = True
currency_name = "CHF" currency_name = "CHF"
@ -47,24 +47,26 @@ def index(request):
if cf['currencies']: if cf['currencies']:
selection = cf['currencies'] selection = cf['currencies']
request.session['currency'] = selection.id request.session['currency'] = selection.id
currency_name=ExchangeRate_name.objects.get(id=selection.id) currency_name = ExchangeRate_name.objects.get(id=selection.id)
else: else:
request.session['currency'] = None request.session['currency'] = None
if request.session['currency']: if request.session['currency']:
currency = request.session['currency'] currency = request.session['currency']
for idx, article in enumerate(articles_list): for idx, article in enumerate(articles_list):
article.price_in_chf = rate.exchange(currency, article.price_in_chf) article.price_in_chf = rate.exchange(
currency, article.price_in_chf
)
articles_list[idx] = article articles_list[idx] = article
currency_name=ExchangeRate_name.objects.get(id=currency) currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request, return render(request,
'webshop/index.html', 'webshop/index.html',
{'category_list': category_list, {'category_list': category_list,
'articles_list': articles_list, 'articles_list': articles_list,
'currencies_form': currencies_form, 'currencies_form': currencies_form,
'article_view': article_view, 'article_view': article_view,
'currency_name': currency_name}) 'currency_name': currency_name})
def articles_in_category(request, category_id): def articles_in_category(request, category_id):
@ -74,7 +76,7 @@ def articles_in_category(request, category_id):
category=selected_category.id).exclude(status=get_hidden_status_id()) category=selected_category.id).exclude(status=get_hidden_status_id())
articles_list = list(articles) articles_list = list(articles)
currencies_form = CurrenciesForm currencies_form = CurrenciesForm
rate=ExchangeRate rate = ExchangeRate
article_view = True article_view = True
currency_name = "CHF" currency_name = "CHF"
@ -88,16 +90,17 @@ def articles_in_category(request, category_id):
if cf['currencies']: if cf['currencies']:
selection = cf['currencies'] selection = cf['currencies']
request.session['currency'] = selection.id request.session['currency'] = selection.id
currency_name=ExchangeRate_name.objects.get(id=selection.id) currency_name = ExchangeRate_name.objects.get(id=selection.id)
else: else:
request.session['currency'] = None request.session['currency'] = None
if request.session['currency']: if request.session['currency']:
currency = request.session['currency'] currency = request.session['currency']
for idx, article in enumerate(articles_list): for idx, article in enumerate(articles_list):
article.price_in_chf = rate.exchange(currency, article.price_in_chf) article.price_in_chf = rate.exchange(
currency, article.price_in_chf)
articles_list[idx] = article articles_list[idx] = article
currency_name=ExchangeRate_name.objects.get(id=currency) currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request, 'webshop/category.html', return render(request, 'webshop/category.html',
{'articles_list': articles_list, {'articles_list': articles_list,
@ -111,7 +114,8 @@ def articles_in_category(request, category_id):
def article_details(request, article_id): def article_details(request, article_id):
category_list = get_categories() category_list = get_categories()
currencies_form = CurrenciesForm currencies_form = CurrenciesForm
rate=ExchangeRate amount = AddToCartForm
rate = ExchangeRate
article_view = True article_view = True
currency_name = "CHF" currency_name = "CHF"
@ -122,20 +126,37 @@ def article_details(request, article_id):
picture_list = Picture.objects.filter(article=article_id) picture_list = Picture.objects.filter(article=article_id)
if request.method == 'POST': if request.method == 'POST':
currencies_form = CurrenciesForm(request.POST) print(request.POST)
if currencies_form.is_valid(): # hier wird das Currency dropdown bearbeitet:
cf = currencies_form.cleaned_data if 'currencies' in request.POST:
if cf['currencies']: currencies_form = CurrenciesForm(request.POST)
selection = cf['currencies'] print("currencies_form")
request.session['currency'] = selection.id if currencies_form.is_valid():
currency_name=ExchangeRate_name.objects.get(id=selection.id) cf = currencies_form.cleaned_data
else: if cf['currencies']:
request.session['currency'] = None selection = cf['currencies']
request.session['currency'] = selection.id
currency_name = ExchangeRate_name.objects.get(
id=selection.id)
# hier wird der Artikel in den Wahrenkorb transferiert:
if 'amount' in request.POST:
amount = AddToCartForm(request.POST)
print("add_to_cart_form")
if amount.is_valid():
print("is valid")
af = amount.cleaned_data
if af['amount']:
amount = af['amount']
print("amount:", amount, "article_id:", article_id)
amount = AddToCartForm()
else:
amount = AddToCartForm()
if request.session['currency']: if request.session['currency']:
currency = request.session['currency'] currency = request.session['currency']
article.price_in_chf = rate.exchange(currency, article.price_in_chf) article.price_in_chf = rate.exchange(currency, article.price_in_chf)
currency_name=ExchangeRate_name.objects.get(id=currency) currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request, 'webshop/article_details.html', return render(request, 'webshop/article_details.html',
{'article': article, {'article': article,
@ -143,7 +164,10 @@ def article_details(request, article_id):
'currencies_form': currencies_form, 'currencies_form': currencies_form,
'article_view': article_view, 'article_view': article_view,
'currency_name': currency_name, 'currency_name': currency_name,
'picture_list': picture_list}) 'picture_list': picture_list,
'amount': amount
})
@login_required @login_required
def profile(request): def profile(request):