-
-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subscribe to newsletters using ajax requests #263
Comments
Sounds like a great feature. If you're happy to implement it, it's hereby on the roadmap. ^^ Also take a look at https://django-newsletter.readthedocs.io/en/latest/usage.html#embed-a-sign-up-form-within-any-page - perhaps this can be incorporated. |
Yes, I've looked at that in detail and I also am able to ajax submit, ignoring the response, but this is not a robust solution to this. With the ajax submit, the backend still redirects to the splash page, and renders a template in the ajax response. In the current, we would have to parse out the returned html to figure out what happened on the submission, and after the redirection. What I think we want here, is to get back a json success message or error depending on what happens with the request. I'll see if I can get this working, and will submit a PR if so. I guess another solution would be to wrap the form in a Django Rest Framework view by creating an API endpoint to the newsletter model. This perhaps could be a bit cleaner. Do you have an opinion on how you would solve this in your own project? Thanks! |
Hi @dokterbob I've put a bit of work into this, and basically the solution looks like the following in
This is sort of hacked together just to see what's possible. The one thing I'm struggling about, is where we should hold JSON strings? Should we store in templates or in source code? I'm guessing templates would generalize this a bit more and just render to string. Here is the html:
And the javascript using axios.
|
Quickly skimmed through this. One thing I noticed, which has nothing to do with this, is that newsletter subscription without confirmation, especially in light of very sensible EU regulation, seems like a real antifeature and I would like to suggest to remove it soon. With regards to strings; note that we currently use the messages framework. Therefore, I suggest we essentially use the same views, except we return a JSON representation of the messages: {"messages": ["one", "more", "message"]} It might even make sense to have the different message classes also included, this way it becomes obvious for an AJAX user what type of message they're getting (regardless of return status, although it's important to have that too). There might be some code doing this floating around the web. Example: {
"success":[],
"failure":[],
"info": []
} When doing AJAX, make sure to take CSRF into account, it's a bit of a hassle from what I remember. Also note that there is already a Only in case of an actual server error should it return I suggest you make a start in a branch based on this. Do expect that some of the things might need to be changed - but I can read the code much better if it's properly formatted and/or diff'able against the original. |
I'm thinking of putting together a PR on this. If I understand correctly, the following would be accepted? On the ActionRequestView:
|
I forked this repo and implemented a django rest framework endpoint for my own purposes. GET Request: Gets the subscription of signed in user to all newsletters.
Returns: [
{
"user": "585c0a22-0e70-42f5-afa5-05958a99c149",
"newsletter": {
"id": 1,
"title": "Announcements",
"slug": "announcements"
},
"subscribed": false
},
{
"user": "585c0a22-0e70-42f5-afa5-05958a99c149",
"newsletter": {
"id": 2,
"title": "Blog",
"slug": "blog"
},
"subscribed": false
},
] POST Request: Sets subscription to newsletters for signed in user.
Request body: [
{
"newsletter": 2,
"subscribed": true
},
{
"newsletter": 3,
"subscribed": true
}
] |
I see that there has been some discussion around being able to subscribe to a newsletter via ajax and in multiple places on the site. However, it does not seem like anyone has yet created an ajaxified view such as what is described here in the Django docs. In order to wrap views from outside the project is a bit clunky. It would make sense to have these views be able to handle ajax subscribe. Is there anything on the roadmap to add this feature?
The text was updated successfully, but these errors were encountered: