Created
May 22, 2018 01:05
-
-
Save djwesleyborges/0af242aed47d0fbeac623f8128ef719f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from advertisement.models import Advertisement | |
class AdvertisementForm(forms.ModelForm): | |
class Meta: | |
model = Advertisement | |
fields = [ | |
'title', | |
'address', | |
'complement', | |
'number', | |
'zip_code', | |
'value', | |
'description', | |
'category', | |
'image', | |
] | |
widgets = { | |
'title': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Title', 'required': ''}), | |
'address': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Address', 'required': ''}), | |
'complement': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Complement', 'required': ''}), | |
'number': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Number', 'required': ''}), | |
'zip_code': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Zip-Code', 'required': ''}), | |
'value': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Value 00,00', 'required': ''}), | |
'description': forms.TextInput( | |
attrs={'class': 'form-control', 'placeholder': 'Description', 'required': ''}), | |
'image': forms.FileInput( | |
attrs={'class': 'form-control-file', 'placeholder': 'Image', 'required': ''}), | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.contrib.auth.models import User | |
# Create your models here. | |
class Advertisement(models.Model): | |
title = models.CharField(max_length=100) | |
address = models.CharField(max_length=100) | |
complement = models.CharField(max_length=100) | |
number = models.CharField(max_length=5) | |
zip_code = models.CharField(max_length=9) | |
value = models.CharField(max_length=8) | |
description = models.TextField() | |
image = models.ImageField(upload_to='media/%Y/%m/%d/', default='', verbose_name='Imagem') | |
created_at = models.DateTimeField('Created on: ', auto_now_add=True) | |
update_at = models.DateTimeField('Update on: ', auto_now=True) | |
created_by = models.ForeignKey(User, null=True, on_delete=models.CASCADE) | |
category = models.ForeignKey(Category, verbose_name='Category') | |
def __str__(self): | |
return self.title | |
class Category(models.Model): | |
name = models.CharField('Name', max_length=100) | |
slug = models.SlugField('Unique identifier', max_length=100) | |
created = models.DateTimeField('Created on', auto_now_add=True) | |
modified = models.DateTimeField('Modified on', auto_now=True) | |
class Meta: | |
verbose_name = 'Category' | |
verbose_name_plural = 'Categories' | |
ordering = ['name'] | |
def __str__(self): | |
return self.name | |
def get_absolute_url(self): | |
return reverse('catalog:category', kwargs={'slug': self.slug}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load static %} | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>{% block title %}{% endblock %}</title> | |
<link rel="stylesheet" href="{% static 'css/bulma.css' %}"> | |
<link rel="stylesheet" href="{% static 'site.css' %}"> | |
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.css' %}"> | |
</head> | |
<div class="container"> | |
<div class="columns is-mobile"> | |
<div class="column is-half is-offset-one-quarter"> | |
<body class="text-center"> | |
<div class="navbar-menu" id="nav-data"> | |
<div class="navbar-end"> | |
<div class="navbar-item"> | |
<div class="field is-grouped"> | |
<p class="control"> | |
<span> | |
</span> | |
</a> | |
</p> | |
</div> | |
</div> | |
</div> | |
</div> | |
<form method="POST" class="text-center" enctype="multipart/form-data"> | |
{% csrf_token %} | |
<img class="mb-4" src="{% static 'img/1.PNG' %}" alt="" width="150" height="150"> | |
<h1 class="h3 mb-3 font-weight-normal">Register you product</h1> | |
{{ form.title }} | |
{{ form.address}} | |
{{ form.complement }} | |
{{ form.number }} | |
{{ form.zip_code }} | |
{{ form.value }} | |
{{ form.description }} | |
<select> | |
{% for ctg in category %} | |
<option name="category" value="{{ ctg.name }}">{{ ctg.name }}</option> | |
{% endfor %} | |
</select> | |
<br> | |
Send image: | |
{{ form.image }} | |
<div class="cor_Error"> | |
{% if form.errors %} | |
{{ form.non_field_errors }} | |
{% endif %} | |
</div> | |
</br> | |
<button class="btn btn-lg button is-primary btn-block" type="submit">Save</button> | |
<button class="btn btn-lg button is-primary btn-block" type="reset">Cancel</button> | |
</a> | |
<p class="mt-5 mb-3 text-muted">© Wesley Borges - 2018</p> | |
</form> | |
</body> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def registerAdvertisement(request): | |
template_name = 'product/registration.html' | |
form = AdvertisementForm() | |
category = Category.objects.all() | |
if request.method == "POST": | |
form = AdvertisementForm(request.POST, request.FILES) | |
if form.is_valid(): | |
advertisement = form.save(commit=False) | |
advertisement.created_by = request.user | |
advertisement.save() | |
return redirect(reverse_lazy('account:home')) | |
context = {'form': form, 'category': category} | |
return render(request, template_name, context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment