Posts

Showing posts from December, 2020

add slug in display urls

 step1:  add slugfield in models.py             class Post(models.Model):     title = models.CharField(max_length=200)     description = models.CharField(max_length=200)     content=models.CharField(max_length=2000)     image=models.ImageField(default='tt.jpg',blank=True,upload_to='postimage')     date=models.DateField(default=timezone.now)     slug=models.SlugField(max_length=200,blank=True,null=True)          def __str__(self):         return self.title  step2:make a function in models.py        def slug_generator(sender,instance,*args, **kwargs):     if not instance.slug:       #  instance.slug='SLUG'       instance.slug=unique_slug_generator(instance) step3: connect database class and upper functions in models.py            pre_save...

add --> viewpost/1/ <------------- like this in url for any post i.e. continue reading

 step1:in template inside for loop  send id to the post you want to display url      {% for post in post %} <div class="post"> <div class="posttitle"> <span></span> <div class="title">{{post.title}} </div></div> <div class="postbody"> <div class="postdscimg"> <div class="postimg"><img src="{{post.image.url }}"></div> <div class="postdsc">{{post.description}}</div> </div> <div class="postcontent">{{post.content|slice:"0:100"}}.....<a href=" viewpost/{{post.id}} "><span>continue reading</span></a> </div> <div class="publisheddate">published on:{{post.date}}</div> </div> </div> {% endfor %} step 2: add URL in urls.py                 path('viewpost/<i...

creating media directory and fetching images to the templates

 https://matthiasomisore.com/web-programming/display-image-in-a-django-template-using-imagefield/

what is the video for the querries thatt you are searching in code with harry?

 how to pass list to template     video no.34

changimg or editing existing models?

 say your current models is     class Product(models.Model):            product_id=models.AutoField            name= models.CharField(max_length=50)           desc=models.Charfield(max_length=300)           pub_date=models.DateField() if you want to add category image and subcategory                class Product(models.Model):            product_id=models.AutoField            name= models.CharField(max_length=50)           desc=models.Charfield(max_length=300)           pub_date=models.DateField()   ///you must add default values otherwise while making migration terminal will ask you some unnecessary things////           category=models.CharField(max_length=50,default='')     ...

how to not register user if they entered already registered password,email or username

 in views.py in thsi example from form two password are taken to verify      you might be confused regarding message see message section blog post you will understand def user_register(request):     if request.method=="POST":         fname= request.POST.get('firstname')         lname= request.POST.get('lastname')         username=request.POST.get('username')         email=request.POST.get('email')         pass1=request.POST.get('pass1')         pass2=request.POST.get('pass2')         if pass1!=pass2:             messages.warning(request,'password does not match')             return redirect('register')         elif User.objects.filter(username=username).exists():             messages.warning(request,'username al...

how to show message alert in django template

 step1: in views.py------> from django.contrib import messages step2: in views.py--------> inside function you created for a particular task; i.e . if you want to submit form and want message " your form is successfully authorized"       then inside function before : return redirect(auth.html)  write   : message. message_type(request,'your form is successfully authorized') step3: if you want to redirect that page to auth.html then go to auth.html and    {% block body%}  {% for message in messages %} < div class = "alert alert-{{message.tags}} alert-dismissible fade show" role = "alert" > < strong >helo ! </ strong > {{message}} < button type = "button" class = "btn-close" data-bs-dismiss = "alert" aria-label = "Close" ></ button > </ div > {% end for%} {% endblock body%}

how to take input from form and save to database django

 step1: complete your HTML and CSS form    eg : <form class="Form1" method="post" action="/"> {% csrf_token %}   <label for="name"></label><br>   <input type="text" id="name" name="name" placeholder="Name">      <label for="email"></label><br>   <input type="text" id="email" name="email" placeholder="email">   <label for="subject"></label><br>   <input type="text" id="subject" name="subject" placeholder="subject">   <label for="message"></label><br>   <input type="text" id="messaeg" name="message" placeholder="message">   <button type="submit"><i class="fa fa-paper-plane"></i> SEND MESSAGE</button> </form> ste...