Author Picture

Kim Majali


Handle Multiple Forms On One Page

Author Avatar wrote on 02/06/2022

Template in form

Read more

CRUD Generic Class-Based Views

Author Avatar wrote on 02/06/2022

1 models.py


from django.contrib.gis.db import models

class  Contact(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    address = models.CharField(max_length=100)
    phone = models.CharField(max_length=50)
Read more

Create Objects

Author Avatar wrote on 02/06/2022

Using save

p1 = Person(first_name="Vuyisile", last_name="Ndlovu")
p1.save()
Read more

Update Objects

Author Avatar wrote on 02/06/2022

Update one record

    try:
        record = Account.objects.filter(id=1).first()
        record.balance = new_amount
        record.save()
        messages.success(request, "Company info has been updated." )
    except Exception as e:
        messages.error(request, "Something went wrong.")
        print("Exception Update balance ==========================")
        print(e)
        print("End of Exception  ==========================")

Read more

Django Stripe Integration

Author Avatar wrote on 02/06/2022

1. Install
pip install stripe
2. Get Keys
1. go to https://dashboard.stripe.com/ 2. developers --> "API keys"
3. Add keys to settings.py
STRIPE_PUBLISHABLE_KEY = '' STRIPE_SECRET_KEY = ''
4. Test code
5. Pass key to view

from django.conf import settings

class HomePageView(TemplateView):    
     template_name = 'home.html'
     def get_context_data(self, **kwargs):     
          context =super().get_context_data(**kwargs)        
          context['key'] = settings.PUBLISHABLE_KEY        
          return context
 
6. Change to test code to form to act when success
7. Create template templates/payment_success.html
8. urls.py

path('charge/', views.charge, name='payment_success'), 
 
9. View
def charge(request):
    if request.method == 'POST':
        charge = stripe.Charge.create(
            amount=500,
            currency='usd',
            description='A Django charge',
            source=request.POST['stripeToken']
        )
        return render(request, 'payment_success.html')
 
Test cards
https://stripe.com/docs/testing #cards number: 4242424242424242 cvc: any 3 digits expiry: Any future date
10. Check test logs
Checkout Docs
Subscription
https://www.ordinarycoders.com/blog/article/django-stripe-monthly-subscriptionRead more

Topics: ModelsIntegrations

CRUDgen Django CRUD generator