Wednesday, August 23, 2023

Re: Error while showing detailed post

Hi Raghavan,

Thank you so much for the response, 

I have fixed the issue, But still facing one issue in uploading images while adding the post, 

I have created one model for the post 
class post(models.Model):
   
    title=models.CharField(max_length=200)
    author=models.CharField(max_length=100)
    Discription=models.CharField(max_length=350)
    Detailed_post=models.CharField(max_length=1500)
    img=models.ImageField(upload_to='media')
    def __str__(self):
        return self.title

And the I user model form to display the from in frontend 

class postForm(ModelForm):
    class Meta:
        model=post
        fields=["title",'author','Discription', 'Detailed_post','img']      
        widgets = {
            'title': TextInput(attrs={'class': 'input-name', 'style':'width:400px; '}),
            'author':TextInput(attrs={'class': 'input-author', 'style':'width:400px; '}),
            'Discription':Textarea(attrs={'class': 'input-Discription', 'style':'width:400px; '}),
            'Detailed_post':Textarea(attrs={'class': 'input-Detailed_post', 'style':'width:400px; '}),
           
        }




and my view is  
def add_post(request):
    if request.method=="POST":
        form=postForm(request.POST)
       
        if form.is_valid():
            form.save()
            return redirect('/News_post')
        else:  
            return HttpResponse("<h1>There is some Error your Post is not added</h1>")
    else:
        form=postForm()
    return render(request, 'add_post.html',{'form':form})


When submitting the form it's aware of hitting the else part, and when i remove the img its works fine





Thanks & Regard Dhiraj Kumar
Mobile No. : 9643843355, EXT: 250




On Tue, Aug 22, 2023 at 9:59 PM Mvd Raghava <mvdr20@gmail.com> wrote:
what issue are you facing. please send the error

On Sat, Aug 19, 2023 at 9:03 PM Dhiraj Kumar <dhiraj@nimbusitsolutions.com> wrote:
Hi new in Django, 

I want to display details post to the template but facing issue while trying to fetch post by id on the click of Continue Reading
Views.py

def show(request, id):
    print(id)
    try:
        showPost = post.objects.get(id=id)  # Assuming your model is named 'post'
    except post.DoesNotExist:
        # Handle the case where the post with the given ID doesn't exist
        raise Http404("Post does not exist")

    return render(request, 'show_post.html', {'showPost': showPost})




Urls.py 
from django.urls import path
from news import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('',views.home, name='home' ),
    path('about', views.about, name="about"),
    path('contact', views.contact, name="contact"),
    path('News_post', views.News_post, name='News_post'),
    path('add_post',views.add_post,name='add_post'),
    path('show/<int:id>/', views.show, name='show')

]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)










Thanks & Regard Dhiraj Kumar
Mobile No. : 9643843355, EXT: 250


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAdfw9TdfFir%2BiFUyGxAhzWLdpDCtfSZr2ZPhi7DJrUauJUn-w%40mail.gmail.com.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFfOSBnje%3DqkUBaa8ZFrLLZ7Fme7-DWE-px1LpbT06b3yvmHKg%40mail.gmail.com.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAdfw9S3MpiBeebBRaO62kEkWg_wj82KY56C2zEpJn82L3kTvw%40mail.gmail.com.

No comments:

Post a Comment