diff --git a/src/products/forms.py b/src/products/forms.py index 2d083cf..cd23a27 100644 --- a/src/products/forms.py +++ b/src/products/forms.py @@ -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( @@ -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"})) diff --git a/src/products/views.py b/src/products/views.py index 5cbdeac..e1feb78 100644 --- a/src/products/views.py +++ b/src/products/views.py @@ -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() diff --git a/src/trydjango/urls.py b/src/trydjango/urls.py index fd04b53..33ca15f 100644 --- a/src/trydjango/urls.py +++ b/src/trydjango/urls.py @@ -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), ]