Error while showing detailed post

34 views
Skip to first unread message

Dhiraj Kumar

unread,
Aug 19, 2023, 11:34:00 AM8/19/23
to django...@googlegroups.com
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


Mvd Raghava

unread,
Aug 22, 2023, 12:30:21 PM8/22/23
to django...@googlegroups.com
what issue are you facing. please send the error

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

Dhiraj Kumar

unread,
Aug 23, 2023, 3:01:54 AM8/23/23
to django...@googlegroups.com
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; '}),
           
        }

image.png


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

image.png



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


Reply all
Reply to author
Forward
0 new messages