when I am querying for the job_seeker profile and If there is no job_seeker data then I was getting an error JobSeeker models query does not exist. I instead want to pass empty list if there is no data. For this, i tried the following way but i am getting an error so could not pass the custom response
class JobSeekerNode(DjangoObjectType):
class Meta:
model = models.JobSeeker
interfaces = (relay.Node, )
class JobSeekerQueries(ObjectType):
job_seeker = Field(JobSeekerNode)
def resolve_job_seeker(self, info, **kwargs):
data = {}
if info.context.user.is_authenticated:
try:
profile = Profile.objects.get(user=info.context.user)
try:
job_seeker = models.JobSeeker.objects.get(profile=profile)
data['job_seeker'] = job_seeker
except:
# when there's no row instead of passing error, pass empty list
data['job_seeker'] = []
return JsonResponse(data)
except Profile.DoesNotExist:
return []
return None
this is the error i get when trying to pass the custom response(empty list if there's no data)
{
"errors": [
{
"message": "Received incompatible instance \"<JsonResponse status_code=200, \"application/json\">\"."
}
],
"data": {
"job_seeker": null
}
}
I even tried this one
def resolve_job_seeker(self, info, **kwargs):
if info.context.user.is_authenticated:
try:
profile = Profile.objects.get(user=info.context.user)
try:
job_seeker = models.JobSeeker.objects.get(profile=profile)
return job_seeker
except:
return models.JobSeeker.objects.none()
except Profile.DoesNotExist:
return []
return None
still i am getting such issue
{
"errors": [
{
"message": "Received incompatible instance \"<QuerySet []>\"."
}
],
"data": {
"job_seeker": null
}
}
if JobSeeker is a model then it should be JobSeeker.objects.filter. convention
You can't return custom things define in your query object.
You are calling get method. objects.get() returns a single object instead of a list.
A simpler solution would be calling filter()
job_seeker = JobSeeker.objects.filter(profile=profile)
return job_seeker
If there are not any matching profile it will return an empty list.
In both cases just return the job seeker.
There is no need for using an extra field like data if this is the thing you want.
def resolve_job_seeker(self, info, **kwargs):
if info.context.user.is_authenticated:
try:
profile = Profile.objects.get(user=info.context.user)
job_seeker = JobSeeker.objects.filter(profile=profile)
return job_seeker
except Profile.DoesNotExist:
raise Exception("Profile is not created for this user")
raise Exception("user not logged in")
Related
I have a view that takes 2 arguments :course_slug and chapter_slug and i want to check if the given course and chapter exist in the database so what's the best way to do that :
def Chapter_Detail(request,course_slug,chapter_slug):
try:
course = Course.objects.get(slug=course_slug)
except Course.DoesNotExist:
raise Http404("course does not exist")
try:
chapter = Chapter.objects.get(slug=chapter_slug)
except Chapter.DoesNotExist:
raise Http404("chapter does not exist")
'''
continue view logic
'''
context = {
'chapter':chapter,
'course' :course,
}
return render(request,'courses/chapter-detail.html',context)
or:
def Chapter_Detail(request,course_slug,chapter_slug):
try:
course = Course.objects.get(slug=course_slug)
chapter = Chapter.objects.get(slug=chapter_slug)
'''
continue view logic
'''
except Course.DoesNotExist:
raise Http404("course does not exist")
except Chapter.DoesNotExist:
raise Http404("chapter does not exist")
context = {
'chapter':chapter,
'course' :course,
}
return render(request,'courses/chapter-detail.html',context)
or:
def Chapter_Detail(request,course_slug,chapter_slug):
try:
course = Course.objects.get(slug=course_slug)
chapter = Chapter.objects.get(slug=chapter_slug)
except Course.DoesNotExist:
raise Http404("course does not exist")
except Chapter.DoesNotExist:
raise Http404("chapter does not exist")
else :
'''
continue view logic
'''
context = {
'chapter':chapter,
'course' :course,
}
return render(request,'courses/chapter-detail.html',context)
or there is a better way
continue view logic means that we are going to work with the course and chapter objects
As mentioned by Aiden, you can use the shortcut specifically made for this.
from django.shortcuts import get_object_or_404
def Chapter_Detail(request,course_slug,chapter_slug):
course = get_object_or_404(Course, slug=course_slug)
chapter = get_object_or_404(Chapter, slug=chapter_slug)
'''
continue view logic
'''
context = {
'chapter':chapter,
'course' :course,
}
return render(request,'courses/chapter-detail.html',context)
Your three variants will also work of course. The else-block in the third example is strictly not necessary, since either error will get raised or code will proceed.
I am getting a ValueError that the class below didn't return any httpresponse when i try to redirect to a template. the redirect is supposed to go to the stripe payment view.
here is an entire class that has the redirect call
class CheckoutView(View):
def get(self, *args, **kwargs):
form = forms.CheckoutForm()
context = {
'form': form
}
return render(self.request, "checkout.html", context)
def post(self, *args, **kwargs):
form = forms.CheckoutForm(self.request.POST or None)
try:
equipment_order = models.EquipmentOrder.objects.get(user=self.request.user, ordered=False)
if form.is_valid():
street_address = form.cleaned_data.get('street_address')
apartment_address = form.cleaned_data.get('apartment_address')
country = form.cleaned_data.get('country')
zip = form.cleaned_data.get('zip')
'''
TODO: add functionality to these fields
same_shipping_address = form.cleaned_data.get('same_shipping_address')
save_info = form.cleaned_data.get('save_info')
'''
payment_option = form.cleaned_data.get('payment_option')
billing_address = models.BillingAddress(
user=self.request.user,
street_address=street_address,
apartment_address=apartment_address,
country=country,
zip=zip
)
billing_address.save()
equipment_order.billing_address = billing_address
equipment_order.save()
if payment_option == 'S':
return redirect('create:payment', payment_option='stripe')
elif payment_option == 'P':
return redirect('create:payment', payment_option='paypal')
else:
messages.warning(self.request, "Invalid payment option")
return redirect('create:checkout')
except ObjectDoesNotExist:
messages.error(self.request, "You do not have an active order")
return redirect("create:order_summary")
1) Remove the try/except i think its better
2) I think you have a problem on 'payement_option' , maybe it doesnt give any value of expected , try to print it first to see what does it give
3) remove the ' or None ' from CheckoutForm
4) you can avoid using 'self' by importing form in that way :
from .forms import CheckoutForm
...
form = CheckoutForm(request.POST)
The above answer may work fine but as I tried your code it throws the same error as you described whenever you leave the form field empty or no payment method is selected.
After trying your code the best possible solution I figure out is this. I know this is not a perfect solution but it worked 😅
Suggestion: Try to move your else statement under if instead of nesting it after elif statement. And change your else to given below.
Old:
else:
messages.warning(self.request, "Invalid payment option select")
return redirect('core:checkout')
New:
else :
messages = 'Invalid payment option select'
return HttpResponse(messages)
Proof: Invalid payment option select
I'm currently learning Django however I'm torn on how to structure the equivalent of add method using it. I'm creating a URL shortener and I'm between the following methods to implement in creating the shortened URL:
def shorten(request):
if request.method == 'POST':
http_url = request.POST.get("http_url","")
if http_url: # test if not blank
short_id = get_short_code()
new_url = Urls(http_url=http_url, short_id=short_id)
new_url.save()
return HttpResponseRedirect(reverse('url_shortener:index'))
else:
error_message = "You didn't provide a valid url"
return render(request, 'url_shortener/shorten.html', { 'error_message' : error_message })
return render(request, 'url_shortener/shorten.html')
vs.
def shorten(request):
http_url = request.POST["http_url"]
if http_url:
short_id = get_short_code()
new_url = Urls(http_url=http_url, short_id=short_id)
new_url.save()
return HttpResponseRedirect(reverse('url_shortener:index'))
else:
error_message = "You didn't provide a valid url"
return render(request, 'url_shortener/shorten.html', { 'error_message' : error_message })
return render(request, 'url_shortener/shorten.html')
Specifically, I want to know the best practice on the following:
Is it best practice to explicity test if method is post or http_url = request.POST["http_url"] is enough
Is http_url = request.POST.get("http_url","") recommended to be used or this is just suppressing the error?
If (2) is not recommended, how can I make the http_url required and throw an error? I also tried the following but the except block is not triggered when I submit a blank form
def shorten(request):
if request.method == 'POST':
try:
http_url = request.POST["http_url"]
short_id = get_short_code()
new_url = Urls(http_url=http_url, short_id=short_id)
new_url.save()
return HttpResponseRedirect(reverse('url_shortener:index'))
except KeyError:
error_message = "You didn't provide a valid url"
return render(request, 'url_shortener/shorten.html', { 'error_message' : error_message })
return render(request, 'url_shortener/shorten.html')
request.POST["key"]
will throw a KeyError when the key is not present in the dictionary. You can use a try...catch clause to handle the error.
Generally though, its idiomatic and perfectly normal to do:
request.POST.get("key")
More about get here.
I'm calling a method using reverse but I have problem argument that I am not able to understand.
My error :
Reverse for 'shopping.views.payment_confirmation' with arguments '(35,)' and keyword arguments '{}' not found.
My url:
url(r'^payment_confirmation/(?P<id>\d+\d{2\})/$', 'payment_confirmation', name='payment_confirmation'),
My view:
def show(request):
...
...
payment.submit(settings.SITE_URL + reverse("shopping.views.payment_confirmation", args=[payment.id]))
My model Payment:
class Payment(models.Model):
...
...
def submit(self, redirect_url):
'''
Sends self as a Payment through PagSeguro API.
Payment instance MUST BE SAVED before calling this method.
'''
if not self.id:
#Temporary to identify which problem caused the crash.
raise ValidationError
#creating a reference if its None
if self.reference is None:
self.reference = configs.PAGSEGURO_REFERENCE_PREFIX + str(self.id)
document = Document()
document.appendChild(self.to_element(document, redirect_url))
response = document2dict(api.submit_payment(document))
try:
self.code = response['checkout']['code']
self.answered_on = datetime.datetime.now()
self.save()
except:
error_str = ""
if type(response["errors"]["error"]) != list:
response["errors"]["error"] = [response["errors"]["error"]]
for error in response["errors"]["error"]:
error_payment = ErrorPayment()
error_payment.code = int(error['code'])
error_payment.payment = self
error_payment.save()
error_str += "[%s: %s]" % (error_payment.code,
error_payment.get_code_display())
raise Exception(error_str)
the error is here payment.submit (settings.SITE_URL + reverse ("shopping.views.payment_confirmation", args = [payment.id]))
I', using this api https://bitbucket.org/leonardosantos/django-pagseguro2/
This line: reverse("shopping.views.payment_confirmation", args=[payment.id]) tells Django to find a url that matches a method called payment_confirmation in the views.py file in the shopping app that will accept a payment ID parameter.
In the error that you shared, the payment.id was 35. The error is saying that either there is no method called payment_confirmation in your shopping.views or the method does not accept a single int as a parameter.
You didn't share a payment_confirmation method in your views file so it seems that that is the issue. You need to add:
def payment_confirmation(payment_id):
#do stuff
to your views.
Hay All, I've got a simple context processor which looks within a session and if a 'user' key exists. If it does i want to return it to the template.
Here's my context Processor
def get_user_details(request):
user = request.session['user']
data = {
'user':user
}
return data
and here is a sample view
def render_home(request):
return render_to_response("home", context_instance=RequestContext(request))
If the session['user'] doesn't exists, i want it to silently fail, or return False or Null.
Because the key doesnt exist within the session, i get a KeyError.
Any idea's how to fix this?
You can get a default value like None this way: request.session.get('user', None). Just like in normal Python dicts.
user = request.session.get('user', None)
or,
user = None
if 'user' in request.session:
user = request.session['user']
def get_user_details(request):
try:
user = request.session['user']
except KeyError:
return
data = {
'user':user
}
return data
Or if you want to catch it further away, do this instead:
def render_home(request):
try:
return render_to_response("home", context_instance=RequestContext(request))
except KeyError:
return