Answer the question
In order to leave comments, you need to log in
How to fix error in django urls?
I want to migrate, but I get the error ImportError: cannot import name 'process_withdraw_transacions' from 'cc.tasks' (/home/www/proj/marketplace/env/lib/python3.9/site-packages/cc/tasks.py) . Maybe an error in the urls?
URLs.py :
from django.urls import path
from .views import CreateWallet, WalletDetail, Withdraw
app_name: 'wallet'
urlpatterns = [
path('create/', CreateWallet.as_view(), name='wallet_create'),
path('wallet/(?P<pk>\d+)/', WalletDetail.as_view(), name='wallet_detail'),
path('wallet/(?P<pk>\d+)/withdraw', Withdraw.as_view()^M, name='wallet_withdraw'),
]
from time import sleep
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, FormView
from django.views.generic import DetailView
from django.shortcuts import get_object_or_404
from django import forms
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.urls import reverse, reverse_lazy
from wallet.models import ProxyWallet
from cc.models import Wallet
from cc.tasks import process_withdraw_transacions
class LoginRequiredMixin(object):
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
class Wallets(LoginRequiredMixin, ListView):
template_name = 'wallet/list.html'
def get_queryset(self):
return ProxyWallet.objects.filter(user=self.request.user)
wallets = Wallets.as_view()
class CreateWallet(LoginRequiredMixin, CreateView):
model = Wallet
fields = ['currency', 'label']
template_name = 'wallet/create.html'
success_url = reverse_lazy('wallet:list')
def form_valid(self, form):
response = super(CreateWallet, self).form_valid(form)
ProxyWallet.objects.create(
user=self.request.user,
wallet=self.object
)
return response
create = CreateWallet.as_view()
class WalletDetail(LoginRequiredMixin, DetailView):
model = ProxyWallet
template_name = 'wallet/detail.html'
def get_queryset(self):
qs = super(WalletDetail, self).get_queryset()
return qs.filter(user=self.request.user)
detail = WalletDetail.as_view()
class Withdraw(LoginRequiredMixin, FormView):
template_name = 'wallet/withdraw.html'
def get_form_class(self):
class WithdrawForm(forms.Form):
address = forms.CharField()
amount = forms.DecimalField()
execute = forms.BooleanField(required=False)
return WithdrawForm
def form_valid(self, form):
px = get_object_or_404(ProxyWallet, user=self.request.user, **self.kwargs)
self.px = px
try:
px.wallet.withdraw(form.cleaned_data['address'], form.cleaned_data['amount'])
except ValueError as e:
form.add_error(None, e)
return self.form_invalid(form)
if form.cleaned_data['execute']:
process_withdraw_transacions.delay(ticker=px.wallet.currency.ticker)
sleep(0.2)
return super(Withdraw, self).form_valid(form)
def get_success_url(self):
return reverse('wallet_detail', kwargs={'pk': self.px.pk})
withdraw = Withdraw.as_view()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question