#forms
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'email')
def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError('Passwords don\'t match.')
return cd['password2']
#views
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(username=cd['username'], password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return redirect('/blog/')
else:
return render(request,
'account/disabled_password.html')
else:
return render(request,
'account/disabled_password.html')
else:
form = LoginForm()
return render(request, 'account/login.html', {'form': form})
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
# Create a new user object but avoid saving it yet
new_user = user_form.save(commit=False)
# Set the chosen password
new_user.set_password(user_form.cleaned_data['password'])
# Save the User object
new_user.save()
return redirect('/account/login/')
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html', {'user_form': user_form})
url(r'', include('posts.urls',namespace='posts'))
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^post/$', views.post_home, name='post'),
]
from django.db import models
from autoslug import AutoSlugField #module
from taggit.managers import TaggableManager#module
class Article(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateField(auto_now_add=True)
slug = AutoSlugField(populate_from='title', unique_with='pub_date__month')#module
tag = TaggableManager()
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'first_name', 'email')
def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError('Passwords don\'t match.')
return cd['password2']
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
# Create a new user object but avoid saving it yet
new_user = user_form.save(commit=False)
# Set the chosen password
new_user.set_password(user_form.cleaned_data['password'])
# Save the User object
new_user.save()
profile = Profile.objects.create(user=new_user)
return redirect('/account/login/')
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html', {'user_form': user_form})
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
position_lat = models.DecimalField(decimal_places=8, max_digits=10, verbose_name="Широта")
position_long = models.DecimalField(decimal_places=8, max_digits=10, verbose_name="Довгота")
def __str__(self):
return 'Profile for user {}'.format(self.user.username)
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'first_name', 'email')
def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError('Passwords don\'t match.')
return cd['password2']
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
# Create a new user object but avoid saving it yet
new_user = user_form.save(commit=False)
# Set the chosen password
new_user.set_password(user_form.cleaned_data['password'])
# Save the User object
new_user.save()
return render(request,
'template_done.html',
{'new_user': new_user})
else:
user_form = UserRegistrationForm()
return render(request, 'template.html', {'user_form': user_form})
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
class Article(models.Model):
title = models.CharField(max_length=100)
text = models.TextField()
image = models.ImageField(upload_to='images/', blank=True, null=True)
#is_published = models.BooleanField(default=True)
def image(request):
images = Article.objects.all()# тут objects
return render(request, 'news/index.html',{'images':images})
{% for im in images %}
<div class="list_one">
<p>{{ im.title }}</p>
<img src="{{ im.image.url }}>
{% endfor %}