Saturday, August 27, 2016

Not upoading image files in django

from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404,redirect
from .forms import PostForm,profileform
from .models import Profile

# Create your views here.
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post_list.html',{'posts':posts})


def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})

def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})

def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published_date = timezone.now()
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})


def SaveProfile(request):
saved = False

if request.method == "POST":
#Get the posted form
MyProfileForm = ProfileForm(request.POST, request.FILES)

if MyProfileForm.is_valid():
profile = Profile()
profile.name = MyProfileForm.cleaned_data["name"]
profile.picture = MyProfileForm.cleaned_data["picture"]
profile.save()
saved = True
return redirect('showProfile')
else:
MyProfileForm = ProfileForm()

return render(request, 'saved.html', locals())


def showProfile(request):
profiles = Profile.objects.all()
return render(request,'blog/profiles.html',{'profiles':profiles})


def addNotes(request):
if request.method == "POST":
form = profileform(request.POST,request.FILES)
if form.is_valid():
print('done')
# profile = Profile()
# profile.name = form.cleaned_data["name"]
# profile.picture = form.cleaned_data["picture"]
post = form.save(commit=False)
#post.published_date = timezone.now()
profile.save()
return redirect('showProfile')
else:
form = profileform()
return render(request, 'blog/newNotes.html', {'form': form})from django.db import models
from django.utils import timezone


class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)

def publish(self):
self.published_date = timezone.now()
self.save()

def __str__(self):
return self.title

class Profile(models.Model):
name = models.CharField(max_length = 50)
picture = models.ImageField(upload_to = 'blog/media')

class Meta:
db_table = "profile"
from django import forms
from .models import Profile
from .models import Post,Profile

class PostForm(forms.ModelForm):

class Meta:
model = Post
fields = ('title', 'text',)

# class ProfileForm(forms.Form):
# name = forms.CharField(max_length = 100)
# picture = forms.ImageField()

class profileform(forms.ModelForm):

class Meta:
model = Profile
fields = ('name','picture')
Image file is not getting uploaded using Django my all model,view and forms files are attached with this link

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b0d0dbbf-d8d6-45e8-96fe-1f720bd2051a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment