Skip to content

Commit

Permalink
28 - Initial Values for Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Jul 11, 2018
1 parent 324bc7b commit f694270
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
17 changes: 0 additions & 17 deletions src/products/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class ProductForm(forms.ModelForm):
title = forms.CharField(label='',
widget=forms.TextInput(attrs={"placeholder": "Your title"}))
email = forms.EmailField()
description = forms.CharField(
required=False,
widget=forms.Textarea(
Expand All @@ -30,22 +29,6 @@ class Meta:
'price'
]

def clean_title(self, *args, **kwargs):
title = self.cleaned_data.get("title")
if not "CFE" in title:
raise forms.ValidationError("This is not a valid title")
if not "news" in title:
raise forms.ValidationError("This is not a valid title")
return title

def clean_email(self, *args, **kwargs):
email = self.cleaned_data.get("email")
if not email.endswith("edu"):
raise forms.ValidationError("This is not a valid email")
return email




class RawProductForm(forms.Form):
title = forms.CharField(label='', widget=forms.TextInput(attrs={"placeholder": "Your title"}))
Expand Down
28 changes: 28 additions & 0 deletions src/products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@
from .models import Product
# Create your views here.

def render_initial_data(request):
initial_data = {
'title': "My this awesome title"
}
obj = Product.objects.get(id=1)
form = ProductForm(request.POST or None,instance=obj)
if form.is_valid():
form.save()
context = {
'form': form
}
return render(request, "products/product_create.html", context)

















# def product_create_view(request):
# my_form = RawProductForm()
Expand Down
3 changes: 2 additions & 1 deletion src/trydjango/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
from django.urls import path

from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view
from products.views import product_detail_view, product_create_view, render_initial_data

urlpatterns = [
path('', home_view, name='home'),
path('about/', about_view),
path('contact/', contact_view),
path('create/', product_create_view),
path('initial/', render_initial_data),
path('product/', product_detail_view),
path('admin/', admin.site.urls),
]
Expand Down

0 comments on commit f694270

Please sign in to comment.